amazon media file explorer download upload index folder image licenses menu widget Play Pause profile-settings more dots-two-horizontal dots-two-vertical more-vertical pending google-plus hangouts facebook instagram whatsapp spotify telegram twitter vine renren rss youtube twitch vimeo flickr dribble behance deviantart 500px steam github soundcloud skype reddit linkedin lastfm delicious stackoverflow pinterest xing flattr foursquare yelp World

Understanding & Scheduling Tasks With Cron [Ubuntu]

I previously wrote a tutorial about managing system processes in ubuntu, where we walked through starting, stopping programs, and the likes.

In some cases, you may need an application to perform a task at a specific time rather than always running in the background, this is where cron comes in. By using Cron, you can schedule a process, script, or program to run at a specific time.

Let's get started...

Each user can create a cron job, which is under a crontab, a crontab includes a list of cron jobs(one per line), also, the root user also has crontabs for system-wide administrative task.

Viewing a Crontab of a user

crontab -l

This will show you a list of jobs for the use that executed the above command, if you executes ut as root (sudo), you'll see root crontabs.

Viewing a Crontab of a Specific User

If you want to view a crontab for a specific user, you can use the -u option e.g

sudo crontab -u James -l

You would see the message no crontab for user if you haven't created a cron job.

Creating a Cron job

To create a cron job, log in to the user you want the task to run under, then execute the following command:

crontab -e

Output:

user@server:~$ crontab -e
no crontab for user - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]:




If you have more than one text editor, you may see the above output, I'll go with nano, so, I'll enter 1, you can change it later by running 'select-editor' or by simply running Editor=vim crontab -e, which would take you directly to a text editor.

Once you enter 1, hit enter, and a default crontab file would be opened with some useful info about how cron works, to add a new job, add it at the bottom of the file, each task to run has to be defined through a single line indicating with different fields when the task will be run and what command to run for the task, e.g:

m h dom mon dow command

Each cron job has six fields, each separated by at least one space or tab spaces:

  • The first field shows the minute (m) in which the job would occur,
  • The second field shows the hour (h) in the 24-hour format (0-23) the job would occur,
  • The third field represents the day of the month (dom), if you place 7, that would indicate the 7 of the month
  • The fourth field represent the month (mon), if you place 7, that would correspond to july
  • The fifth field is the day of the week (dow), starting from 0-6, 0 represent Sunday - 6 (Saturday)
  • The last field is the command to be executed

Updating Server Repository Index at 7 Pm Every Friday

* 19 * * 4 /usr/bin/apt-get update

The * asterisk means anytime, so, the above command simply updates the server repo every Friday (4), at 7 pm. You would notice I used the full command path, this is considered best practice. If you don't know what the full command is, you can use the which command.

There are a whole host of other command combos you can try, enjoy!

Related Post(s)

  • How To Access Windows 10 Drive/Folder in VirtualBox

    To access Windows drive or folder in your Guest OS, you first of all, install VirtualBox guest utilities: sudo apt-get install virtualbox-guest-utils You then Add a shared folder by going into: Setti

  • Send Mail with Attachment Using Mutt in GNU/Linux

    Mutt is a powerful text&#x2d;based mail client for Unix/Linux operating systems. It features color support, message threading, MIME support...

  • Using Pageant To Automatically Authenticate SSH key in Putty

    I can't count how many times I have typed my ssh key passphrase whenever my ssh connection times out, it is so annoying and repetitive. Well, thanks to the putty pageant, you can do that seamlessly.

  • Installing WP-CLI In a GNU/Linux Server

    WP-CLI is a command-line interface for WordPress. It can also be used with ClassicPress, as they are no differences in their usage, maybe just minimal if you are updating or downloading new ClassicPr

  • How To Send Mail To Multiple Addresses Using (mailx)

    In this guide, you'll learn a couple of ways you can send mail to multiple addresses using mailx. mailx is a utility program for sending and receiving mail. I assume you already have mailx command, i

  • Mounting and Unmounting Cloud Storage With (Rclone) in GNU/Linux

    Ever wondered if you could mount your preferred cloud storage as a virtual drive on your system? Thanks to Rclone, you can mount and access different kinds of cloud storage from a file manager, I'll