How to schedule tasks on Linux

Schedule non-trivial tasks to run whenever you want

Nice! But how can I use it? What are specific use cases?
Or why should I even care about it?

Well, every company I worked before had its routines. Daily, weekly, monthly, on even non-periodic. Most of them were simple tasks you could easily manage through Google Calendar, Wunderlist, Trello, Slack, or whatever, just choose your weapon.

In some cases though, these weapons were not sufficient. Sometimes you have to access your database and check if a stock price raised above an specific value, or monitor if an e-mail message was received and extract its attachment which is a zip file with a password including a spreadsheet with some useful data you need to import to your database and… you get it.

In theeese cases you’ll think about replacing your hummer with a bazooka.

Before getting started

You’ll need to run a Linux terminal. If you’re on Windows and still want to try it, you can use a free cloud solution such as Cloud9 IDE. Go ahead! I’ll wait.

Sign up, log in to your account, and find Create a new workspace.

Fill in the form, choose Private and Custom

Click Create workspace, wait for it to load, and…

We’re good to go!

Create your first scheduled task

  • On the terminal window, type crontab -e to edit your crontab file
    At your first time, Linux will ask you to select an editor, just like below.
  • I am a Vim user, so I’d type 3 and press <Enter>
    It will open up a black screen. That’s your text editor. Don’t be afraid.
  • Type i to enter insert mode on Vim.
  • Type the following line of code:
    * * * * * date >> ~/workspace/log.txt
    It is saying: add the current date to the log.txt file every minute.
    Of course you can use another folder or filename. Go ahead and change it.
  • Press <Escape> to leave insert mode
  • Finally, type :wq and press <Enter> to quit Vim
    Note that w stands for write (or save), and q for quit
  • You’ll see the crontab: installing new crontab message,
    It means your new crontab file was successfully loaded into memory.

Make it run

Let’s check if your cron process is running:

  • Type pidof cron
  • If it outputs nothing, try using sudo cron start
  • Now you should see its process id (pid)

It means it is working! Check your log.txt file by tailing it:

  • tail -f ~/workspace/log.txt (or just tail -f log.txt)

Beautiful, isn’t it? Now let’s learn how to stop it.

Stop your tasks

You can either edit your crontab, deleting the corresponding line, or just hard-kill the running cron process. Let’s see the second method.

  • Start by typing sudo kill `pidof cron`
  • sudo gives you super powers, meaning super user do
  • kill will stop the process given as argument
  • pidof cron gets the id of the cron process

Quick tips on Linux processes

To view a dynamic real-time list of running processes, use the top command. Then press q to quit, or h for help. Note the cron process at the bottom.

Otherwise, you can just simply use ps -A command.

And a few more tips:

  • to get your cron PID in one single command, try pidof cron
  • to kill your cron job, use sudo kill 19604
  • or, altogether, you could just type sudo kill `pidof cron`

Now let’s get going!

Editing tasks

That’s quite the same as before:

  • Just open it by typing crontab -e
  • Now you’re using Vim (or your default text editor)
  • Type i to insert mode
  • Edit the file as needed
  • Then <Escape> :wq <Enter>

More info on crontabs

You can always use the man linux command by typing man cron into your terminal. It will show you a detailed manual on how this crontab works.

To change how frequent your tasks are schedule, you need to have a brief understanding of how the first 5 parameters work. Let’s take a look.

So there are five time arguments, a user, and a command.

  • Minute: use * for every minute, or type the minute you want.
    You can also write multiple comma-separated minutes.
    And also say every five minutes, e.g., by using */5.
  • Hour, Day of Month, Month, Day of Week, follow the same logic.
  • User: might be root, for administrative purposes, or your own user.
  • Command: just use the command you’d type to your terminal.

Otherwise, if you’re in a hurry (or just lazy!), check out the link below: — it’s a simple site to help you build a crontab.

Are you into productivity hot tips for web development? I’m a Brazilian full-stack developer, living and working in Paris, and I regularly share articles like the one you’ve just read. Follow me on twitter to stay tuned.

Brazilian full-stack developer. Living and working in Paris. Sharing productivity hot tips for web development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store