2

So I've just discovered the command setsid, which makes life a whole lot easier for running programs from Terminal. What I'd like to know is if there's a way to set programs to launch in a new session automatically, because honestly I'm probably going to be prepending setsid before everything I run now. Is this bad practice for some reason?

Murmur
  • 201

1 Answers1

1

Apart from creating aliases for your programs, it's not really possible to have setsid automatically prefixed before every command typed in the terminal. However, I find it is very useful to create an .inputrc in your home folder and place in it (observing the quoting):

Control-o: "setsid "

You can now hit Ctrl+o in the terminal and then type your command; it saves a lot of time if you need to keep repeating it. There is no error in the placement of quotes: it allows you to immediately type a command after the shortcut is used. This functionality and much more is provided by bash's readline library: see man bash or the Ubuntu manpages online.

With reference to your second question, it isn't bad practice to use setsid a lot and just depends on your particular needs. In fact it is extremely useful, because, as noted at kernel.org,

setsid creates a new session if the calling process is not a process group leader. The calling process is the leader of the new session, the process group leader of the new process group, and has no controlling tty...The calling process will be the only process in this new process group and in this new session.

Hence the program launched with setsid has complete independence from the terminal from which it was started, as a new process has been forked.

There are also other similar, but different ways of detaching a program from the terminal such as nohup or disown. (It should be said that setsid is a program from the util-linux package, while disown is a bash builtin.) This question may be of great interest, as it touches upon such related topics: