Services and Cron

How to work with cron

Linux execute cron job after system reboot

The crontab command maintains crontab files for individual users. One can install or remove cron jobs as per needs.

Running job at startup (boot time)

You need to use special string called @reboot. It will run once, at startup after Linux reboot command. The syntax is as follows:

@reboot /path/to/job @reboot /path/to/shell.script @reboot /path/to/command arg1 arg2

This is an easy way to give your users the ability to run a shell script or command at boot time without root access. First, run crontab command:
$ crontab -e
OR
# crontab -e -u UserName
# crontab -e -u vivek
Run a script called /home/vivek/bin/installnetkit.sh
@reboot /home/vivek/bin/installnetkit.sh
It i a super-simple way to run scripts on boot using cron jobs.

How to run a cron job automatically after server reboot

Say your script needs to be run only after 5 minutes. For example: reboot + 5mintues. The syntax is as follows:
@reboot sleep 300 && /home/wwwjobs/clean-static-cache.sh
The sleep command to add delay for a specified amount of time. In this example, 600 seconds and after that time clean-static-cache.sh gets executed at boot time.

Start crond automatically at boot time

You also need to enable crond service via sys v / BSD init style system. Under RHEL / CentOS / Fedora, you need to use chkconfig (ntsysv) command to enable crond on boot:

# chkconfig crond on ## Commands to start/stop/restart crond for Linux Execute Cron Job ### # service crond restart # service crond start # service crond stop

Under Debian / Ubuntu Linux use update-rc.d as follows to turn on service on boot:

# update-rc.d cron defaults

If you are using modern distro with systemd, try the following systemctl command

For Debian/Ubuntu Linux with systemd, try:

Save and close the file. For further information read out tutorial on cron jobs.

How do I run a single command at startup using systems (systemctl command)?

Create a .service file that looks something like this

Pay close attention to the “Requires” and “ExecStart” fields.

Requires - this tells systemctl that it cannot proceed with this service until the services (space deliminted) are up and running

After - this tells systemd the order in which the services should bootstrap

Restart - systemd will automatically perform a restart of the service if it fails for any reason

ExecStart=/bin/sh <path to executable|script> - notice the pre process /bin/sh this is important. Without this the application|script will fail to start. Systemd executes as root so need for sudo su

  1. Place it in /etc/systemd/system folder with say a name of neo4j-docker.service - this will create a service called neo4j-docker

  2. Make sure that your script executable with:

  3. Reload systemd

     

  4. Start it:

  5. Enable it to run at boot:

  6. Stop it:

Notes

  1. You don't need to launch start-neo4j-container.sh with sudo in your service, as the default service user is already root.

  2. Look at the links below for more systemd options

Great write up can be found at https://askubuntu.com/questions/919054/how-do-i-run-a-single-command-at-startup-using-systemd

Other systemctl command

Reload the daemon

Disabling a service

Systemctl overall status

Obtain status of a service

List service files

List services files and see the status of service

Removing a service