Some projects can only be run intermittently, which saves a lot of resources. It is convenient to use the built-in cron scheduler for this.
It is important to run crontab as root.
Display current tasks:
crontab -l
Edit tasks:
crontab -e
Considering that most projects must to be launched while being in the same directory with them, you will first have to change directory and only then run the script.
For example, you can run start.sh
every 10 minutes like this:
*/10 * * * * cd /root/projects/project_name && ./start.sh
More examples:
# Every 20 minutes:
*/20 * * * * <command>
# Each hour:
0 */2 * * * <command>
# Every 4 hours:
0 */4 * * * <command>
# Every day at 21:00:
0 21 * * * <command>
# Every Sunday at 00:00:
@weekly <command>
Here you can choose your options:
You should not use cron for this, it simply does not have the necessary functionality. Instead, the commands should be added to the /etc/rc.local
file. This approach is considered obsolete, but it is much simpler than all the others.
Create/edit /etc/rc.local
. (Important! This file may be located in /etc/init.d/rc.local
, then you need to work with it)
nano /etc/rc.local
The template looks like this, paste:
|
|
Don’t remove the commented block at the top, it’s required for update-rc.d
to work. Before exit 0
add something like the example below (where -d
means detach/daemon mode):
cd /root/projects/project_name && ./start.sh -d
You can also run scripts as another user:
cd /root/projects/project_name && sudo -H -u user01 ./start.sh -d
Set file permissions:
chmod +x /etc/rc.local
|
|
If you get the error Failed to enable unit: Unit file rc-local.service does not exist.
when you run the systemctl start rc-local
and systemctl status rc-local
commands, then follow the instructions below.
Create a file:
nano /etc/systemd/system/rc-local.service
With content:
|
|
Then:
|
|
Check that the services are starting by rebooting.