-
Notifications
You must be signed in to change notification settings - Fork 1
S. Cron and Process clean up
Last update: 04/04/2021
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
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.
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.)
Very important to memorise below structure!
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
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)
-
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).
This link summarises pretty well what you need to know so I recommend you check this techmitechmint article.
What we need to know for now and what we will use is: ps -ef
and from it we need to know the header and what those values mean.
Item Description USER User login name PID Process ID PPID Parent process ID C CPU utilization of process STIME Start time of process TTY Controlling workstation for the process TIME Total execution time for the process CMD Command
$ :ps -ef | head
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 15:25 ? 00:00:04 /sbin/init fixrtc splash splash
root 2 0 0 15:25 ? 00:00:00 [kthreadd]
root 3 2 0 15:25 ? 00:00:00 [rcu_gp]
root 4 2 0 15:25 ? 00:00:00 [rcu_par_gp]
root 8 2 0 15:25 ? 00:00:00 [mm_percpu_wq]
root 9 2 0 15:25 ? 00:00:01 [ksoftirqd/0]
root 10 2 0 15:25 ? 00:00:03 [rcu_preempt]
root 11 2 0 15:25 ? 00:00:00 [migration/0]
root 12 2 0 15:25 ? 00:00:00 [idle_inject/0]
Press q to exit from the top session and h to get help.
top gives you a realtime cli view on processes, the same as a "Task Manager" - Windows or "Activity Monitor" - Mac or "System Monitor" - Linux Desktop.
Htop is almost the same as top but you can interact with it by clicking in the CLI.
Let's say a process ends badly or is stuck in a loop when a script is ran, but you want it to stop eating resources (both CPU and RAM), what do you do?
Well you kill it!
And I'm not even joking, but how can we do that?
Well the answer is pretty simple, all we need is the PID - process id.
$ :ps -ef | grep python
root 1976 1 0 15:25 ? 00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root 2049 1 0 15:25 ? 00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
root 2052 1 0 15:25 ? 00:01:16 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
ubuntu 14916 5330 1 18:01 pts/1 00:00:00 watch -n 2 python test
ubuntu 14926 5556 0 18:02 pts/3 00:00:00 grep python
Let's immagine the watch command has gone rogue and we want to stop it.
$ :kill 14916
We search for it again, but it is not there.
$ :ps -ef | grep python
root 1976 1 0 15:25 ? 00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root 2049 1 0 15:25 ? 00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
root 2052 1 0 15:25 ? 00:01:16 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
ubuntu 14999 5556 0 18:03 pts/3 00:00:00 grep python
-
But what if we have 20 or 100 processes that are rogue and need to be killed?
-
Well, for sure we will not kill them one by one, we will use this process_killer.sh script.
But wait! there is more.
What if we don't want to have to run this process everytime we see something bad, what if we want it to be part of the cleanup crew and once your scripts should be run running it comes and sweeps the process table?
Well nothing simpler!!
We schedule it in crontab!
crontab -e
*/10 * * * * /home/ubuntu/Desktop/test.sh 2> /home/ubuntu/Desktop/test_error.log
*/15 * * * * /home/ubuntu/Desktop/process_killer.sh
This way your small Raspberry Pi will never run out of resources.
**Congrats, you're done!**
We have learned about cron, cron tabs and how to schedule cron jobs.
We have also learned about processes and how to view them and kill them.
If you hit a problem or have feedback (which is highly welcomed) please feel free to get in touch, more details in the footer.