top of page
Search

How to create an alias to search in a very long crontab archive

Writer: Mathilde BenedettoMathilde Benedetto

Updated: Oct 27, 2023


Crontab structure
Crontab structure

Suppose that you are a very big company with lots of clients and

you have a crontab with a very long list of jobs.

To go faster you can create your own shortcut in the server, an alias called

for example "cron" that would allow you to make a fast search

of your scheduled jobs by typing a partial client name after the cron alias.



Here is how to do this:


1: Write a small script searching in your crontab, like this:

$ vi /path/to/my/getcron.sh
clientName=$1
if [ "$1" == "" ] ; then read -p "Client Name: " clientName ; fi
echo "------------------------------------------ "
echo "------------ FOUND IN CRONTAB ------------ "
echo "------------------------------------------ "
crontab -l | /bin/grep -i --color=always "## --- " | /bin/grep -i $clientName
echo "------------------------------------------ "
echo "Active on Shell Cron: "
crontab -l | /bin/grep -i --color=always $clientName | /bin/grep -v "#"
echo "------------------------------------------ "
echo "Suspended on Shell Cron: "
crontab -l | /bin/grep -i $clientName | /bin/grep "#" | /bin/grep -v " ---"



2: Do not define alias cron="/path/to/my/script.sh" but do something like:

$ alias cron="syncscroned"

And then:

$ alias syncscroned="/path/to/my/script.sh"

Why? because if you do:

$ which cron
/usr/sbin/cron

-> You find the daemon

3: Example of use:

$ cron client4
------------------------------------------
------------ FOUND IN CRONTAB ------------
------------------------------------------
## --- CLIENT4 account_client4
------------------------------------------
Active on Shell Cron:
04 05 * * * /path/to/my/script_account_client4.sh
------------------------------------------
Suspended on Shell Cron:
#04 05 * * * /path/to/my/oldScript_account_client4.sh

If you do not pass a value after cron, the script will prompt "Client Name: " and wait your input to go on.

Then if your crontab has a pattern like this:

## --- CLIENT1 account_client1
00 04 * * * /path/to/scriptclient1.sh
## --- CLIENT2 account_client2 account2_client2 account3_client2
00 04 * * * /path/to/scriptclient2.sh
00 04 * * * /path/to/script2client2.sh
00 04 * * * /path/to/script3client2.sh
## --- CLIENT3 account_client3
## --- CLIENT4 account_client4

the search will first prompt the commented line with the description of the client (## --- ETC..). Then, according to your data, it will first list the active croned jobs found (/bin/grep -v "#" excludes the commented lines from the search), and then the commented ones (/bin/grep "#"). The specific exclusion (/bin/grep -v " ---") is due to the commented lines that describe, in the crontab, the clients accounts, for example:

## --- CLIENT1 account_client1

This way, we're not repeating the client description we first found in the output when we look for commented crontab lines.

 
 
 

Comments


bottom of page