Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a PHP script via SSH and keep it running after I quit

Tags:

ssh

bots

irc

I'm trying to restart a custom IRC bot. I tried various commands :

load.php  
daemon load.php  
daemon load.php &&  

But that makes the script execute inside the console (I see all the output) and when I quit the bot quits as well.

The bot author only taught me the IRC commands so I'm a bit lost.

like image 219
WaterBearer Avatar asked Jan 17 '23 23:01

WaterBearer


2 Answers

You can install a package called screen. Then, run screen -dm php load.php and resume with screen -dR

This will allow you to run the script in the background, and still be able to use your current SSH terminal. You can also logout and the process will still be running.

like image 102
Clay Freeman Avatar answered Jan 31 '23 02:01

Clay Freeman


Chances are good the shell is sending the HUP signal to all its running children when you log out to indicate that "the line has been hung up" (a plain old telephone system modem reference to a line being "hung up" when disconnected. You know, because you "hang" the handset on the hook...)

The HUP signal will ask all programs to die conveniently.

Try this:

nohup load.php &

The nohup asks for the next program executed to ignore the HUP signal. See signal(7) and the nohup(1) manpages for details. The & asks the shell to execute the program in the background.

Clay's answer of using screen(1) is pretty awesome, definitely look into screen(1) or tmux(1), but I don't think that they are necessary for this problem.

like image 24
sarnold Avatar answered Jan 31 '23 03:01

sarnold