[Unit] Description=My Script Service [Service] User=habib Type=simple TimeoutSec=0 PIDFile=/run/myscript.pid ExecStart=/path/to/myscript.php KillMode=process Restart=on-failure RestartSec=42s SyslogIdentifier=myscript [Install] WantedBy=default.target
Note : put type=simple and DON'T daemonize the process. Rather run the process like any console application and systemd will take care of the rest.
Also put a SysLogIdentifier and both your stdout-stderr will go to system log which can be viewed by running.
journalctl -u myscript
install: rm -f /etc/systemd/system/myscript.service sudo ln -s /path/to/myscript.service /etc/systemd/system/ sudo systemctl enable myscript sudo systemctl start myscript
#!/usr/bin/php
<?
chdir("/path/to/"); //important!
// -- code to run during every startup.
// like cleaning up previous shutdown.
while(1){
// the main task to be carried out in loop.
sleep(20); // always sleep for sometime in the loop,
// even if for 10 mili seconds,
// so as not the overlaod the server.
}