I have a script, which runs my PHP script each X times:
#!/bin/bash while true; do /usr/bin/php -f ./my-script.php echo "Waiting..." sleep 3 done
How can I start it as daemon?
We can use systemd unit files to launch our script at boot. Unit files describe how the system should execute the given program. We set the user that the script will run with the User= option and the path to the script with the ExecStart= option. The various options are documented in the systemd. exec man page.
In computing, a daemon (pronounced DEE-muhn) is a program that runs continuously as a background process and wakes up to handle periodic service requests, which often come from remote processes.
To create a daemon, you need a background process whose parent process is init. In the code above, _daemon creates a child process and then kills the parent process. In this case, your new process will be a subprocess of init and will continue to run in the background.
Use bg to Send Running Commands to the Background You can easily send such commands to the background by hitting the Ctrl + Z keys and then using the bg command. Hitting Ctrl + Z stops the running process, and bg takes it to the background.
To run it as a full daemon from a shell, you'll need to use setsid
and redirect its output. You can redirect the output to a logfile, or to /dev/null
to discard it. Assuming your script is called myscript.sh, use the following command:
setsid myscript.sh >/dev/null 2>&1 < /dev/null &
This will completely detach the process from your current shell (stdin, stdout and stderr). If you want to keep the output in a logfile, replace the first /dev/null
with your /path/to/logfile.
You have to redirect the output, otherwise it will not run as a true daemon (it will depend on your shell to read and write output).
A Daemon is just program that runs as a background process, rather than being under the direct control of an interactive user...
[The below bash code is for Debian systems - Ubuntu, Linux Mint distros and so on]
The simple way:
The simple way would be to edit your /etc/rc.local file and then just have your script run from there (i.e. everytime you boot up the system):
sudo nano /etc/rc.local
Add the following and save:
#For a BASH script /bin/sh TheNameOfYourScript.sh > /dev/null &
The better way to do this would be to create a Daemon via Upstart:
sudo nano /etc/init/TheNameOfYourDaemon.conf
add the following:
description "My Daemon Job" author "Your Name" start on runlevel [2345] pre-start script echo "[`date`] My Daemon Starting" >> /var/log/TheNameOfYourDaemonJobLog.log end script exec /bin/sh TheNameOfYourScript.sh > /dev/null &
Save this.
Confirm that it looks ok:
init-checkconf /etc/init/TheNameOfYourDaemon.conf
Now reboot the machine:
sudo reboot
Now when you boot up your system, you can see the log file stating that your Daemon is running:
cat /var/log/TheNameOfYourDaemonJobLog.log
• Now you may start/stop/restart/get the status of your Daemon via:
restart: this will stop, then start a service
sudo service TheNameOfYourDaemonrestart restart
start: this will start a service, if it's not running
sudo service TheNameOfYourDaemonstart start
stop: this will stop a service, if it's running
sudo service TheNameOfYourDaemonstop stop
status: this will display the status of a service
sudo service TheNameOfYourDaemonstatus status
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With