Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a process in the background - '&' clarification. 1

Status
Not open for further replies.

stla

IS-IT--Management
Mar 12, 2003
190
DE
(Elementary user)

Have I understood the use of the '&' character correctly when used in conjunction with a script.....

Let's suppose I have logged in to the server via SSH and type the following:

cd /path/to/script/
./my_script

Would I be correct in thinking that my script will only execute as long as I maintain the SSH shell connection?

When running the script, should I add the flag like this:

./my_script &

Thus the script will run in the background ie. despite the termination of my SSH connection?

Best regards
 
Not exactly.

The "&" will run the process in the background, but it will terminate if you log out. Both ways you show of running it make your SSH session it's parent process. When you log out, making it's parent die, it will die too.

You need to use the "nohup" command. This will detach it from your process and give it a different parent process. That way when you log out, it will keep running. The command looks like this...
Code:
cd /path/to/script

nohup ./my_script &
You still need the "&" to run it in the background.

If you just do that, any output from the script will go into a file called "nohup.out". To save it somewhere easier to find, do this...
Code:
cd /path/to/script

nohup ./my_script > my_script.log 2>&1 &
That will capture all output from the script in "[tt]my_script.log[/tt]".


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top