Skip to content

S. Cron and Process clean up

Bogdan Tudorache edited this page Apr 1, 2021 · 13 revisions

A. Cron

For those who do not know or for those who want to refresh their memory, as per Wikipedia, cron or cron job is a time-based job scheduler in Unix computer operating systems. It automates system maintenance or administration and it's most suitable for scheduling repetitive tasks.

These tasks can be simple commands (as we will later see) or even complex scripts that need to be ran at certain hours to, let's say, generate new web-scraped files that will be posted to your website.

The actions in the cron are driven by a crontab (cron table) which specifies when and what tasks should run in a given schedule. Users can edit/view/remove crontab through a set of specific commands and even though the file is usually stored in: /var/spool/cron/crontabs/ or in etc/ in separate piece as well, you should never try and edit these files.

ubuntu:etc/ :ls cron*
crontab

cron.d:
anacron  e2scrub_all  php  popularity-contest

cron.daily:
0anacron  apport      bsdmainutils	dpkg	   man-db   popularity-contest
apache2   apt-compat  cracklib-runtime	logrotate  mlocate  update-notifier-common

cron.hourly:

cron.monthly:
0anacron

cron.weekly:
0anacron  man-db  update-notifier-common

1. Setting up crontab

First time users first need to set up the editor of their choice, so if you run $ crontab -l and when it says no crontab for sk - using an empty one, you must set up an editor.

 $ crontab -e

 $ no crontab for sk - using an empty one
Select an editor. To change later, run 'select-editor'.
 1. /bin/nano 
 2. /usr/bin/vim.basic <-- easiest, basic vim.
 3. /usr/bin/vim.tiny
 4. /bin/ed

Choose 1-4 [2]:

Press Shift+X to Exit.

2. Crontab commands

crontab -e Edit crontab file, or create one if it doesn’t already exist.

crontab -l crontab list of cronjobs , display crontab file contents.

crontab -r Remove your crontab file.

crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

3. Crontab structure

Very important to memorise below structure!

crontab_structure

4. Crontab example

I highly recommend using the crontab guru to test your configuration because I know how painful and frustrating is to wait for a cronjob to run and then it does nothing.

* * * * * /home/ubuntu/Desktop/test.sh  <-- run test.sh script every minute

*/10 * * * * /home/ubuntu/Desktop/test.sh  <-- run test.sh script every 10 minutes

*/10 * * * * /home/ubuntu/Desktop/test.sh  > /home/ubuntu/Desktop/test.log <-- run test.sh every 10 minutes and append results to test.log

*/10 * * * * /home/ubuntu/Desktop/test.sh  >> /home/ubuntu/Desktop/test.log <-- run test.sh every 10 minutes and overwrite results to test.log

*/10 * * * * /home/ubuntu/Desktop/test.sh  2> /home/ubuntu/Desktop/test_error.log <-- run test.sh every 10 minutes and write only errors to test_error.log

*/10 * * * * /home/ubuntu/Desktop/test.sh >/dev/null 2>&1 <-- run test.sh every 10 minutes and discard any output

5. Crontab log

Logs are saved in /var/log/syslog. Here you can check if the cron job ran successfully or ended in error or even if it did not run.

You can run the cat command or cat with grep ( cat /var/log/syslog | grep CRON)

    $ cat /var/log/syslog

     Apr  1 17:10:01 ubuntu CRON[11864]: (ubuntu) CMD (/home/ubuntu/Desktop/test.sh  2> /home/ubuntu/Desktop/test_error.log) 

B. Process clean up

1. Processes and how to check them

  • Before we go into the details, let's first answer: What is a process?

  • Simple answer, an instance of a running program is called a process. Every time you run a shell command, a program is run and a process is created for it. Each process in Linux has a process id (PID) and it is associated with a particular user and group account.

Linux is a multitasking operating system, which means that multiple programs can be running at the same time (processes are also known as tasks). Each process has the illusion that it is the only process on the computer. The tasks share common processing resources (like CPU and memory).