10

I'm trying to create a script which starts some programs

startup.sh

knetworkmanager
emesene
keepassx

The problem is that when I run the script, it only starts knetworkmanager. This is because it'll start it, then wait until it's finished. How can I start a program without waiting for it? I don't think I can just add "&" after each command, because all processes will still be killed when the script is finished.

6 Answers 6

16

When the shell that runs the script exits, it send a HUP signal to the processes you started. If those don't catch the signal, they will terminate. So whether just using & is sufficient depends on the application. To be safe, use nohup like that:

nohup your-program >>/dev/null 2>>/dev/null &

See the man pages for nohup and kill for more details or read the wikipedia article about nohup.

3
  • Reading again, it's ambiguous whether to OP is exiting the calling shell or not. He only mentions exiting the script, not exiting the shell or the terminal. Sep 9, 2009 at 20:06
  • reading my answer again, you'll notice that I did say "when the shell that runs the script exits". Assuming this shell has its own virtual terminal (not a subshell or something) this is correct.
    – Kim
    Sep 9, 2009 at 20:29
  • @Kim: That's true. Your answer is fine, I'm just sure if the OP is closing the calling shell or not. Sep 9, 2009 at 20:55
10

No, the processes should not be killed when the script is finished if you use &. Try it.

knetworkmanager &
emesene &
keepassx &
0
2

Based on the example programs you gave, it looks like you're trying to start some programs when KDE launches. If so, take a look at KDE's Autostart feature. This article discusses more and gives instructions about how to add programs to autostart.

1
  • Indeed, that's what I'm trying to do. I fixed it now, thanks. Sep 9, 2009 at 19:52
1

As others have said, use the & operator to put a command into the background at the time the shell runs it.

If you want the component parts to run in order (rather than concurently), but still want the whole to be backgrounded, use a sub-shell like this:

(knetworkmanager; emesene; keepassx) &

or, if you want to operation of each part to be conditional on the sucessful completetion of the previous part use:

(knetworkmanager && emesene && keepassx) &
0

Adding a '&' after each command will definitely work. It would mean that those scripts or programs would continue to run even after the startup.sh finishes.

0

Another option would be to start the process as a daemon, using /sbin/start-stop-daemon

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .