Starting a linux process in the background

10

2

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.

Bart van Heukelom

Posted 2009-09-09T17:41:49.090

Reputation: 1 958

Answers

15

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.

Kim

Posted 2009-09-09T17:41:49.090

Reputation: 2 238

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. – Adam Bellaire – 2009-09-09T20:06:59.640

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 – 2009-09-09T20:29:12.277

@Kim: That's true. Your answer is fine, I'm just sure if the OP is closing the calling shell or not. – Adam Bellaire – 2009-09-09T20:55:22.037

10

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

knetworkmanager &
emesene &
keepassx &

Adam Bellaire

Posted 2009-09-09T17:41:49.090

Reputation: 478

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.

Doug Harris

Posted 2009-09-09T17:41:49.090

Reputation: 23 578

Indeed, that's what I'm trying to do. I fixed it now, thanks. – Bart van Heukelom – 2009-09-09T19:52:24.113

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) &

dmckee --- ex-moderator kitten

Posted 2009-09-09T17:41:49.090

Reputation: 7 311

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.

user10574

Posted 2009-09-09T17:41:49.090

Reputation:

0

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

Abhinav

Posted 2009-09-09T17:41:49.090

Reputation: 2 030