DEV Community

Cover image for Let’s Create a Cron Job
Ali Ogun
Ali Ogun

Posted on • Originally published at Medium

Let’s Create a Cron Job

In this article we will write a bash script and set a schedule with crontab. Students can use this article for “born2beroot” project (from 42 Cursus). My greetings to especially 42-Heilbronn peers. Okay, let’s start real quick.

Bash Script

If you are already familiar with bash, you might skip this part and directly jump next section. (Setting Up Crontab)

Create script file under this directory as: /usr/local/bin/monitoring.sh

Let’s take a look at our script below and dive into it.

#!/bin/bash

arc=$(uname -a)
pcpu=$(grep “physical id” /proc/cpuinfo | sort | uniq | wc -l)
vcpu=$(grep “^processor” /proc/cpuinfo | wc -l)
fram=$(free -m | awk ‘$1 == “Mem:” {print $2}’) 
uram=$(free -m | awk ‘$1 == “Mem:” {print $3}’)
pram=$(free | awk ‘$1 == “Mem:” {printf(“%.2f”), $3/$2*100}’) 
fdisk=$(df -Bg | grep ‘^/dev/’ | grep -v ‘/boot$’ | awk ‘{ft += $2} END {print ft}’)

udisk=$(df -Bm | grep ‘^/dev/’ | grep -v ‘/boot$’ | awk ‘{ut += $3} END {print ut}’)

pdisk=$(df -Bm | grep ‘^/dev/’ | grep -v ‘/boot$’ | awk ‘{ut += $3} {ft+= $2} END {printf(“%d”), ut/ft*100}’)

cpul=$(top -bn1 | grep ‘^%Cpu’ | cut -c 9- | xargs | awk ‘{printf(“%.1f%%”), $1 + $3}’)

lb=$(who -b | awk ‘$1 == “system” {print $3 “ “ $4}’)lvmt=$(lsblk | grep “lvm” | wc -l)

lvmu=$(if [ $lvmt -eq 0 ]; then echo no; else echo yes; fi)
ctcp=$(cat /proc/net/sockstat{,6} | awk ‘$1 == “TCP:” {print $3}’)
ulog=$(users | wc -w)
ip=$(hostname -I)
mac=$(ip link show | awk ‘$1 == “link/ether” {print $2}’) 
cmds=$(journalctl _COMM=sudo | grep COMMAND | wc -l)

wall “ 
       #Architecture: $arc
       #CPU Physical: $pcpu
       #vCPU: $vcpu
       #Total and Used Amount of RAM: $uram/${fram}MB ($pram%)
       #Disk Amount and Use: $udisk/${fdisk}Gb ($pdisk%)
       #CPU Use Rate: $cpul
       #Last Restart Time: $lb
       #LVM State of Use: $lvmu
       #Active Number of Connection: $ctcp ESTABLISHED
       #Number of Users Using the Server: $ulog
       #IP and MAC Addresses: IP $ip ($mac)
       #Number of Used Sudo: $cmds cmd
     ”
Enter fullscreen mode Exit fullscreen mode

So, let’s explain briefly what I wrote above:

  • arc=$(uname -a) → Shows the architecture and kernel version of the current operating system.

  • pcpu=$(grep “physical id” /proc/cpuinfo | sort | uniq | wc -l) → Returns the number of physical processors.

  • vcpu=$(grep “^processor” /proc/cpuinfo | wc -l) → Returns the number of virtual processors.

  • fram=$(free -m | awk ‘$1 == “Mem:” {print $2}’) → Returns the amount of available RAM of the server.

  • uram=$(free -m | awk ‘$1 == “Mem:” {print $3}’) → Returns the amount of RAM used.

  • pram=$(free | awk ‘$1 == “Mem:” {printf(“%.2f”), $3/$2*100}’) → printf(“%.2f”) returns 2 values ​​after the comma and if it is $3/$2*100 then it returns percentage version.

  • fdisk=$(df -Bg | grep ‘^/dev/’ | grep -v ‘/boot$’ | awk ‘{ft += $2} END {print ft}’) → Returns the amount of available storage of the server.

  • udisk=$(df -Bm | grep ‘^/dev/’ | grep -v ‘/boot$’ | awk ‘{ut += $3} END {print ut}’) → Returns the server’s used storage space.

  • pdisk=$(df -Bm | grep ‘^/dev/’ | grep -v ‘/boot$’ | awk ‘{ut += $3} {ft+= $2} END {printf(“%d”), ut/ft*100}’) → (storage used / accessible space * 100) gives us percentage usage.

  • cpul=$(top -bn1 | grep ‘^%Cpu’ | cut -c 9- | xargs | awk ‘{printf(“%.1f%%”), $1 + $3}’) → Returns the CPU utilization rate as a percentage.

  • lb=$(who -b | awk ‘$1 == “system” {print $3 “ “ $4}’) → Returns the last reboot date and time.

  • lvmt=$(lsblk | grep “lvm” | wc -l) → Returns the information of disks configured with LVM.

  • lvmu=$(if [ $lvmt -eq 0 ]; then echo no; else echo yes; fi) → Indicates whether LVM is active in the system. P.S: You need to install the net-tools package for other items to run smoothly.

  • ctcp=$(cat /proc/net/sockstat{,6} | awk ‘$1 == “TCP:” {print $3}’) → Returns the current number of active connections.

  • ulog=$(users | wc -w) → Returns the number of users using the server.

  • ip=$(hostname -I) → Gives the server’S IP Address.

  • mac=$(ip link show | awk ‘$1 == “link/ether” {print $2}’) → Gives the server’s MAC Address.

  • cmds=$(journalctl _COMM=sudo | grep COMMAND | wc -l) → Returns the number of commands run with sudo. (If sudo is logged in, it is written with the number of sudo usage of other users; otherwise, it gives as much information as the sudo command used by the current user.)

  • wall “some message” → is a command-line utility that displays a message on the terminals of all logged-in users. The messages can be either typed on the terminal or the contents of a file.

Add the rule that script could be executed without sudo password

Open sudoers file:

$ sudo visudo
Enter fullscreen mode Exit fullscreen mode

Add this line:

$ your_username ALL=(root) NOPASSWD: /usr/local/bin/monitoring.sh
Enter fullscreen mode Exit fullscreen mode

And the reboot the system with :

$ sudo reboot
Enter fullscreen mode Exit fullscreen mode

You can check if your script works well with this command:

$ sudo /usr/local/bin/monitoring.sh
Enter fullscreen mode Exit fullscreen mode

Okay we completed the script. Now we gotta setup the crontab for scheduling it.

Setting Up Crontab

Open the crontab with:

$ sudo crontab -u root -e
Enter fullscreen mode Exit fullscreen mode

At the end of the file add this rule as it shown in Figure-1:

$ */10 * * * * /usr/local/bin/monitoring.sh
Enter fullscreen mode Exit fullscreen mode

Figure-1

And finally use chmod +x to make your script executable:

$ chmod +x monitoring.sh
Enter fullscreen mode Exit fullscreen mode

And you are ready to go. You should have such an output every 10 minutes:

Well done!

Top comments (0)