I need to have a script execute (bash or perl or php, any will do) another command and then exit, while the other command still runs and exits on its own. I could schedule via at command, but was curious if there was a easier way.
-
Is scheduling via cron an option? – Alberto Zaccagni Apr 28 '10 at 17:06
-
You may wish to elaborate on your needs, because the correct answer may depend on it ... (1) what process or user action will launch the script? (2) why do you want a script rather than launch the command directly? (3) do you need to log output, detach from the terminal and/or start a new process group for the command? (4) any other comments? – NVRAM Apr 28 '10 at 18:03
7 Answers
#!/bin/sh
your_cmd &
echo "started your_cmd, now exiting!"
Similar constructs exists for perl and php, but in sh/bash its very easy to run another command in the background and proceed.
edit
A very good source for generic process manipulation are all the start scripts under /etc/init.d
. They do all sorts of neat tricks such as keep track of pids, executing basic start/stop/restart commands etc.

- 3,960
- 3
- 28
- 34
-
One thing that caught me out related to this - you can't do... [ $var -eq 1 ] && your_cmd & ...because the parent process will keep running. – chrism1 Jan 18 '12 at 22:09
-
You may need to use `nohup` to keep `your_cmd` from dying after you exit the shell; see camh's answer. – reinierpost May 13 '15 at 10:15
To run a command in the background, you can append an '&' to the command.
If you need the program to last past your login session, you can use nohup.
See this similar stackoverflow discussion: how to run a command in the background ...

- 1
- 1

- 1,978
- 1
- 19
- 38
The usual way to run a command and have it keep running when you log out is to use nohup(1)
. nohup
prevents the given command from receiving the HUP signal when the shell exits. You also need to run in the background with the ampersand (&) command suffix.
$ nohup some_command arg1 arg2 &

- 40,988
- 13
- 62
- 70
&
?
#!/usr/bin/bash # command1.sh: execute command2.sh and exit command2.sh &

- 117,087
- 18
- 149
- 283
-
2How did you get a four horned unicorn, with all four horns on its head? – Tim Post Apr 28 '10 at 17:16
-
2
I'm not entirely sure if this is what you are looking for, but you can background a process executed in a shell by appending the ampersand (&) symbol as the last character of the command.
So if you have script, a.sh
and a.sh needs to spawn a seperate process, like say execute the script b.sh, you'd:
b.sh &

- 181
- 5
So long as you mentioned Perl:
fork || exec "ls";
...where "ls" is anything at all. Repeat for as many commands as you need to fire off.

- 458
- 5
- 12
-
Or, just `exec "ls";` if don't need the current process to continue. And advantage of this approach is getting STDOUT output sent to the console (as though you had typed the command from the command-line).\ – Brent Faust Jan 08 '15 at 21:58
Most answers are correct in showing..
mycmd &
camh's answer goes further to keep it alive with nohup.
Going further with advanced topics...
mycmd1 &
mycmd2 &
mycmd3 &
wait
"wait" will block processing until the backgrounded tasks are all completed. This can be useful if response times are significant such as for off-system data collection. It helps if you an be sure they will complete.
How do I subsequently foreground a process? If it is your intent to foreground a process on a subsequent logon, look into screen or tmux.
screen -dmS MVS ./mvs
or (Minecraft example).
screen -dm java -Xmx4096M -Xms1024M -jar server.jar nogui
You can then re-attach to the terminal upon subsequent login.
screen -r
The login that launches these need not be interactive, you can use ssh remotely (plink, Ansible, etc.) to spawn these in a "drive by" manner.

- 1,545
- 1
- 12
- 19