You are on page 1of 7

Linux Basics: 20 Useful Crontab Examples in Linux

Cron (aka Crontab) is a task scheduler in Linux that helps to execute a task on a scheduled time, and it is very
similar to Windows Task Schedulers.

With crontab, we can schedule repetitive tasks as well as one-time tasks using @ utility.

Crontab is mostly used for executing backup scripts for taking the system backups and also sometimes to start and
stop applications.

Crontab
Install Crontab
The package name of crontab in CentOS / RHEL is cronie and cron in Ubuntu / Debian. You can install
crontab using the below command.

### CentOS / RHEL ###

yum -y install cronie

### Debian / Ubuntu ###

apt-get install cron

Start Crontab
If the crontab is not running, then start it using the following command.

service crond start

Edit crontab
To add or edit crontab jobs of a current logged in user, use the below command. The command will open a file
editor where you can update the jobs.

crontab -e
To edit other users crontab jobs, use the below command.

crontab -e -u raj

Crontab path
System wide crontab entries are found in /etc/cron{tab,.d,.daily,.hourly,.monthly,.weekly} and individual
users crontab jobs are found in /var/spool/cron/ directory.

Crontab logs
Crontab logs can be found in /var/log/cron

Crontab format / Crontab time format


Below illustration shows you the crontab time format.

Crontab Examples in Linux – Crontab Time Format

20 Useful Crontab Examples


Let us start with very basic crontab example.

1. Schedule a crontab at particular time (at 1 am)


This cron will be useful for you to do some cleanup activities on servers when there is a less usage.

0 1 * * * /path/to/script.sh
This job runs every day 1 AM.

2. Schedule a cron to run twice a day


Do you have a script that is required to be run twice a day? Use the below cron job example.

0 10,22 * * * /path/to/script.sh
Above cron runs twice a day at 10 AM and 10 PM.

3. Schedule a cron to run every Sunday 1 AM


You may need to schedule a cron to do weekend activities like taking full backup or configuration backup.

0 1 * * sun /path/to/script.sh
OR

0 1 * * 0 /path/to/script.sh
OR

0 1 * * 7 /path/to/script.sh
0 or 7 means Sunday.

4. Schedule a crontab every minute


This one may be funny; sometimes you may require run cron for every minute.

* * * * * /path/to/script.sh

5. Schedule a crontab every 5 minutes


Sometimes you may need to run a program like pinging servers for their availability.

*/5 * * * * /path/to/script.sh

6. Schedule a crontab every hour (hourly cron)


Below schedule runs every hour and is used for the hourly task.

0 * * * * /path/to/script.sh
OR

@hourly /path/to/script.sh

7. Schedule a crontab every 2 hours


You can use the below cron job example that set to run a script every two hours.

0 */2 * * * /path/to/script.sh

8. Schedule a crontab daily (daily cron)


Below cron job example will be suitable if you want the script to be executed on a daily basis, exactly at @ 12 AM.

0 0 * * * /path/to/script.sh
OR

@daily /path/to/script.sh

9. Schedule a crontab every alternate day


Use the below cron example to run a job at every alternate day.

0 0 */2 * * /path/to/script.sh

10. Schedule a crontab on select days


To schedule a cron job on select days, i.e., to run cron on Tuesday and Thursday at 1 PM.

0 13 * * tue,thu /path/to/script.sh

11. Schedule a crontab every week (weekly cron)


You can quickly schedule a weekly cron job using below example.

@weekly /path/to/script.sh
This cron runs every week Sunday at 12 AM.

12. Schedule a cron on the 15th day of every month


You can use the below settings if you want a cron job to be executed on 15th of every month.

0 11 15 * * /path/to/script.sh

13. Schedule a cron every month (monthly cron)


You may want to create a cron job that runs on the first day of the month. This cron runs on 1st of every month at 12
AM.

@monthly /path/to/script.sh

14. Schedule a cron on select month


Below cron example runs every day at 12 AM in January, April, and June.

0 0 * jan,apr,jun * /path/to/script.sh

15. Schedule crontab after every reboot


Want to run script or command after every reboot then below job could be useful for you.

@reboot /path/to/script.sh

16. Send email in crontab


You can use the below cron settings for sending results of the scheduled task.

MAILTO="raj"
1 1 * * * /path/to/script.sh

17. Change shell in cron


Want to execute the cron on different shell rather than the default, /bin/bash.

SHELL=/bin/sh
1 1 * * * /path/to/script.sh

18. Environmental variables in cron


Sometimes you may need to use environmental variables for successful execution of script then below setting could
be useful for you.

PATH=/sbin:/bin:/usr/sbin:/usr/bin
1 1 * * * /path/to/script.sh
19. Set Home for cron
Do you want to set the home directory to use when executing scripts? Then use this.

HOME=/
1 1 * * * /path/to/script.sh

20. Cron jobs Every Second


Cron cannot be used to schedule a job in seconds interval.

That’s All. You can also read cron manual for more information.

Configure VNC Server on CentOS 7 / RHEL 7


By Raj Last updated Dec 10, 2017

29

5122

VNC Server on CentOS 7


The following simple guide helps you to setup the VNC server on RHEL 7/CentOS 7 machines, VNC help to share
the desktop with the other machines which have a client installed. VNC server and client are not installed by default,
you need to setup the vnc server manually.

Before installing the VNC server, let us install Gnome desktop using YUM command.

yum groupinstall "GNOME Desktop" "Graphical Administration Tools"


Install the tigervnc server and X11 fonts.

yum install tigervnc-server xorg-x11-fonts-Type1


Copy the VNC server configuration file to /etc/systemd/system/ for configuring the system service. While copying,
you can mention which port it should listen. By default VNC server listens on 5900, but if you setup port-offset for
VNC; we can run a service on a sub-port of the default port 5900. For example, if we mention port 5, it will be
accessible on 5905. You can connect the VNC server by specifying ipaddress:sub-port (192.168.2.109:5 or
192.168.2.109:5905)

cp /lib/systemd/system/vncserver@.service /etc/systemd/system/vncserver@:5.service
Edit the copied file.

vi /etc/systemd/system/vncserver@:5.service
At the end of the file, you will find the lines like below.

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
Replace <USER> with your real user, in my case, i replaced with a user called “raj” .

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/sbin/runuser -l raj -c "/usr/bin/vncserver %i"
PIDFile=/home/raj/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
[Install]
WantedBy=multi-user.target
Add firewall rules to allow the VNC connection, the following rule is set as per the port-offset mentioned in the
previous step.

firewall-cmd --permanent --zone=public --add-port=5905/tcp


firewall-cmd --reload
Start VNC server after switching to raj.

[raj@server ~]$ vncserver


Set the password.

You will require a password to access your desktops.

Password:
Verify:
xauth: file /home/raj/.Xauthority does not exist

New 'localhost.localdomain:1 (raj)' desktop is server.itzgeek.com:1

Creating default startup script /home/raj/.vnc/xstartup


Starting applications specified in /home/raj/.vnc/xstartup
Log file is /home/raj/.vnc/server.itzgeek.com:1.log
Reload the systemctl daemon as root.

systemctl daemon-reload
Start the VNC service as root.

systemctl start vncserver@:5.service


Enable it on system startup as root.

systemctl enable vncserver@:5.service


Take remote desktop.

ipaddress:5905 or ipaddress:5
Configure VNC Server on CentOS 7 – VNC
Connection
You will be asked to enter the password, enter the password that you have created earlier.

Configure VNC Server on CentOS 7 – VNC


Authentication
Now you will get the vnc desktop session.

Configure VNC Server


on CentOS 7 – VNC Desktop
That’s all. You have successfully configured VNC server on CentOS 7 / RHEL 7.

You might also like