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

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay