2

I am writing a shell script that will perform 2 tasks. One task will start up a program that must keep running continuously. Within the same script, the 2nd task is to be initiated that will start up another program. But when the first is running continuously, how can i start another within the same script ?

Thanks in advance.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Cygnus
  • 3,222
  • 9
  • 35
  • 65

2 Answers2

5

You're probably looking to background a task. Generally, you do this like so:

sleep 30 &
sleep 30 &
jobs
echo 'Waiting for all jobs to complete.'
wait

The backgrounding is handled by the & symbol at the end of the line.

See Also

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • But will this work if the script is being executed remotely via Putty ? – Cygnus Jun 29 '12 at 07:16
  • As long as you're connected. See [nohup](http://www.gnu.org/software/coreutils/manual/html_node/nohup-invocation.html) if you want it to remain running after you log out. – Todd A. Jacobs Jun 29 '12 at 07:18
  • @Cygnus: Note: nohup is not enough. You must make sure all input/output from the script (and any commands) is untied from the shell (so you need to redirect output/error/input to files (/dev/null is usefull for this)). – Martin York Nov 10 '13 at 17:29
2

Add an ampersand after the command:

~$ myprogram&

It will place the program running in the background.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195