DEV Community

Mahbub Ferdous Bijoy
Mahbub Ferdous Bijoy

Posted on

Corn-jobs with some custom script

How to Automate Scripts by Scheduling via cron Jobs:

Cron is a job scheduling utility present in Unix like systems. You can schedule jobs to execute daily, weekly, monthly or in a specific time of the day. Automation in Linux heavily relies on cron jobs like network troubleshooting, process managment,cpu and memory management day to day same time.

Below is the syntax to schedule crons:

* * * * * sh /path/to/script.sh

sh represents that the script is a bash script and should be run from /bin/bash.
/path/to/script.sh specifies the path to script

Here, (*) represents minute(s) hour(s) day(s) month(s) weekday(s), respectively.

  1. 1th (*) for : minutes -> 0-59
  2. 2nd (*) for : hour -> 0-24
  3. 3rd (*) for : date -> 1-31
  4. 4th (*) for : month -> 1-12
  5. 5th (*) for : days -> 0-6

5 0 8 : at 00.05 in august
5 4 6 : at 4.05 on sunday
0 22 1-5 : At 22:00 on every day-of-week from Monday through Friday.

most important crontab command:

  1. edits crontab entries to add, delete, or edit cron jobs: $ crontab -e
  2. list all the cron jobs for the current user: $ crontab -l
  3. list another user's crons: crontab -u userName -l
  4. edit another user's crons: crontab -u username -e

Example:

#! /bin/bash

#make a date-script.sh file 

echo date >> date-output.txt

# make this file executabel:

chmod 774 date-script.sh

# add this in crontab for every minutes:

crontab -e 

#and paste this line:
*/1 * * * * sh date-script.shFilePath
#now do cat in date-output.txt you will see all the output

Enter fullscreen mode Exit fullscreen mode

nodeHealth.sh

#!/bin/bash

#####################################################

# Author : Mahbub 
# Date : 04/10/24
# Tags: Check node health
# Description: This script outputs the node health
# Version : v1

######################################################

#debug mode for know which output which command:
set -x 

#print the disk space
df -h

#print the memory 
free -g

#print the resources like cpu
nproc

Enter fullscreen mode Exit fullscreen mode

check_process_running.sh:

#!/bin/bash

#####################################################

# Author : Mahbub 
# Date : 07/10/24
# Tags: Check all processes
# Description: This script outputs the all runing process
# Version : v1

######################################################

#set debug mode for human readable
set -x
#when script get an error :
set -e    # exit the script when there is an error
set -o pipefail  # if we use pipe operator and its have problem without this line command will executed


#check process command:
ps -ef

# check human readable:
ps -h

#if you want to know selected PID(processID) using querythan we can use pipe(|) and (grep)

#ps -ef | grep "processName"
ps -ef | grep "zsh"
#or we can get output in a txt file: here we use backtic for commands 

# echo ps -h | grep "processId" >> ps-output.txt
echo ps -h | grep "zsh" >> zsh-output.txt
#must use backtick after echo command
# awk command for get info column by column :

ps -ef | grep zsh | awk -F" " '{print $2}'   # here $2 means 2nd column

Enter fullscreen mode Exit fullscreen mode

get_error_from_logfile.sh:

#!/bin/bash

#####################################################

# Author : Mahbub 
# Date : 09/10/24
# Tags: Find ERROR in logfile from remote storage 
# Description: This script helps to get error log from remote logfile 
# Version : v1

######################################################

#add debug mode
set -x
set -e
set -o pipefail

# in general logfile are big and also important thats why every company put it on amazon s3 or google drive or github. i mean any remote storage service they use 

# to get error from remote log file we use curl :

curl https://www.ibm.com/docs/en/zos/2.4.0?topic=problems-example-log-file | grep WARNING

# here after grep we can use INFO,WARNING,ERROR,TRACE

Enter fullscreen mode Exit fullscreen mode

Top comments (0)