Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup cron jobs on centos

Centos cron jobs

  1. Let's create a directory this purpose.

# cd

[root@server ~]# mkdir crontests

[root@server ~]# cd crontests

  1. Sample script for testing

[root@server crontests]# vim job.sh

#!/bin/bash

today=$(date)

touch /root/crontests/"$today"
  1. Make script executable*

[root@server crontests]# chmod +x job.sh

  1. Test bash script

[root@server crontests]# ./job.sh

[root@server crontests]# ll
total 4
-rwxr-xr-x 1 root root 60 Jul  8 10:28 job.sh
-rw-r--r-- 1 root root  0 Jul  8 10:58 Mon Jul  8 10:58:01 EAT 2019

You should have a file created with the current timestamp

  1. Register new job

[root@server crontests]# crontab -e

Add the following snippet to the file. You can determine the run intervals using crontab.guru

# Run this job every minute
* * * * * /root/crontests/job.sh
  • Verify job is registered
[root@server crontests]# crontab -l

# Testing centos cron jobs
* * * * * /root/crontests/job.sh
  1. Validate job is running
[root@server crontests]# ll
total 4
-rwxr-xr-x 1 root root 60 Jul  8 10:28 job.sh
-rw-r--r-- 1 root root  0 Jul  8 10:58 Mon Jul  8 10:58:01 EAT 2019
-rw-r--r-- 1 root root  0 Jul  8 10:59 Mon Jul  8 10:59:01 EAT 2019
-rw-r--r-- 1 root root  0 Jul  8 11:00 Mon Jul  8 11:00:01 EAT 2019
-rw-r--r-- 1 root root  0 Jul  8 11:01 Mon Jul  8 11:01:01 EAT 2019
-rw-r--r-- 1 root root  0 Jul  8 11:02 Mon Jul  8 11:02:01 EAT 2019

You should have a file created every minute with the current timestamp

Source:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.