Nov 26, 2020
3 min read
Task scheduling with cron and rc.local

Cron

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:

https://crontab.guru/

rc.local: Automatic execution of commands after system startup

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#! /bin/bash
### BEGIN INIT INFO
# Provides:          my-start-script
# Required-Start:    \$local_fs \$syslog
# Required-Stop:     \$local_fs \$syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts my-start-script
# Description:       starts my-start-script using start-stop-daemon
### END INIT INFO

# put your script here

exit 0

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
1
2
3
systemctl daemon-reload
systemctl start rc-local
systemctl status 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local

[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99

[Install]
 WantedBy=multi-user.target

Then:

1
2
3
systemctl enable rc-local
systemctl start rc-local.service
systemctl status rc-local.service

Check that the services are starting by rebooting.