43

I have a shell script that is enabled as service to start multiple shell scripts e.g.

service started script -> script1, script2 ,script3 

script1 should open a program in a tmux window, and it does work just fine if I manually start the script via ./script1.sh, however when started at boot via the service started script it does not with the above error:

open terminal failed: not a terminal

Why is this happening?

gitaarik
  • 42,736
  • 12
  • 98
  • 105
STiGYFishh
  • 708
  • 3
  • 8
  • 16
  • Well a service would traditionally not have an associated tty (terminal). Its a multi user OS, no one is logged in yet, whose terminal would it write to? Why do these services need an interactive shell? – tolanj Aug 08 '14 at 16:39
  • There are many scripts doing different things backups etc... however the one in question runs a minecraft server in a tmux window – STiGYFishh Aug 08 '14 at 16:46
  • Okay well post the tmux bits of the script, by default it will try to attach to the current tty (which doesn't exist hence the error) if you have a new session in there for example you'll need to add a -d param to prevent this – tolanj Aug 08 '14 at 16:57
  • tmux new -s minecraft "java -args minecarft.jar nogui" – STiGYFishh Aug 08 '14 at 17:22
  • Try tmux new -d -s minecraft "java -args minecarft.jar nogui" – tolanj Aug 08 '14 at 18:43

3 Answers3

52

There is an answer already here, but this link I think summarises it better. In a nutshell, use the -t flag:

ssh -t host tmux attach

If you want to set it into your .ssh/config file, look in the ssh_config manpage for the RequestTTY option:

 RequestTTY
         Specifies whether to request a pseudo-tty for the session.  The
         argument may be one of: ``no'' (never request a TTY), ``yes''
         (always request a TTY when standard input is a TTY), ``force''
         (always request a TTY) or ``auto'' (request a TTY when opening a
         login session).  This option mirrors the -t and -T flags for
         ssh(1).
Community
  • 1
  • 1
lorenzog
  • 3,483
  • 4
  • 29
  • 50
  • 5
    While this answer has answers I was googling for, the question doesn't actually involve SSH; it's about startup scripts. – Weaver Sep 13 '16 at 14:43
18

I think the issue is that the service does not have an associated tty. A workaround I've found is to change your tmux call in your script to

tmux new-session -s username -d

(username a user for whom the service was started)

7hibault
  • 2,371
  • 3
  • 23
  • 33
  • the option `-d` doesn't always work (I have a case with a remote server that fails with the same error: **`open terminal failed: not a terminal`**) – Cyril Chaboisseau Aug 23 '22 at 07:07
  • For sure results vary, this worked for me though. I was trying to run tmux at startup while using .bash_profile to start a openbox kiosk. – Terrarium Sep 13 '22 at 02:16
1

If you use p10k as a zsh theme, as mentioned here, you should put this stanza in the top of .zsrc:

if [ -z "$TMUX" ]; then
  exec tmux new-session -A -s workspace
fi