Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executing a script which runs even if i log off

Tags:

bash

shell

unix

So, I have a long running script (of order few days) say execute.sh which I am planning to execute on a server on which I have a user account...

Now, I want to execute this script so that it runs forever even if I logoff or disconnect from the server?? How do i do that? THanks

like image 459
frazman Avatar asked Oct 27 '25 15:10

frazman


1 Answers

You have a couple of choices. The most basic would be to use nohup:

nohup ./execute.sh

nohup executes the command as a child process and detaches from terminal and continues running if it receives SIGHUP. This signal means sig hangup and will getting triggered if you close a terminal and a process is still attached to it.

The output of the process will getting redirected to a file, per default nohup.out located in the current directory.


You may also use bash's disown functionality. You can start a script in bash:

./execute.sh

Then press Ctrl+z and then enter:

disown

The process will now run in background, detached from the terminal. If you care about the scripts output you may redirect output to a logfile:

./execute.sh > execute.log 2>&1

Another option would be to install screen on the remote machine, run the command in a screen session and detach from it. You'll find a lot of tutorials about this.

like image 50
hek2mgl Avatar answered Oct 29 '25 07:10

hek2mgl