Crontab update

Hello.

We have a big crontab file where we need to comment out for few countries and also uncomment out based on situations . Could someone let us know how this can be done using a script .

Sample file look like & here i need to comment/uncomment based on country code .

0-59 ... u/app/atms/us/bin/location  MX K1
0-59 ... u/app/atms/us/bin/location  GB K1
0-59 ... u/app/atms/us/bin/location   US K1

Thanks
Ron T

You can use sed or awk to change the file. However that is not 100% safe. Some few older UNIXes do not "see" changes to crontab files unless the crontab -e [username] command is used. Not vi. Not sed. Not awk.

What OS and version do you have? -- the output of

 uname -a

is perfect.

Once we know what operating system you're using (as requested by jim McNamara), we'll also need to know what shell you're using.

And, we need to know how you plan to tell your script which country code or country codes are to be processed and whether the invocation of your script is supposed to comment out that country code or make the entry for that country code active.

What are the "situations" you mentioned? How can your script determine whether or not each such "situation" exists?

Will the crontab file to be updated be the crontab file of the user running this script? If not, how will the user running your script get permission to modify the crontab file of the user whose contab file it is that you want to update?

If you can show us what you have already tried to solve this problem on your own, it would help us understand what you are trying to do and where you are stuck.

1 Like

Thanks . We are having AIX ,Linux Boxes & Shell is bash..

We would want to comment specific lines in the crontab file based on the country code & we would need to uncomment the lines based on the country code . for e.g MX is mexico, GB is england ..

We nowus crontab -e and does the editting manually to make the changes . For e.g if we waant to comment out let says 100 process running in different countries then it is time consuming, any help in this would be greatly appreciated .

Ron T

Hello ron5174,

It's a slight bug bear of mine, but GB is for Great Britain. England is a part of that nation state.

Kind regards,
Robin - actually living in England, but proud to be British

You can edit the crontab file directly using any tool/script provided you know that, of its own accord, it will not see the changes immediately. As long as you have your wits about you, and understand what's going on, you can manipulate crontabs.

See my post#4 on this thread:
Rename inside the crontab file

my bad , GB is for Great Britain :slight_smile: So what i am getting is that we need to manually update the crontab file and there is no other way we can do this . is my understanding correct ?

1 Like

No, I don't think anyone has said that it cannot be done using a tool/editor/script automatically. The decision is whether to use crontab command in a script or use another tool to edit the crontab directly followed by a restart of the cron daemon.

You still haven't described the "conditions" you mentioned in post #1 in this thread and you still haven't said how you would like to tell your script what you want it to do on a particular invocation. Without this information, I don't see how we can suggest a script that will do what you want.

Note that although you can't easily run vi with a script, you can easily run ed with a script. So your script might want to consider using:

EDITOR=ed crontab -u user -e <<-EOF
	ed commands to add/remove # from appropriate lines
EOF

but the ed commands you put in that here-document will depend on what "conditions" have been detected, how you want to tell your script which country codes to enable and/or disable, etc.

If you're not comfortable writing ed commands (which you should be if you're used to running vi ), you can also use:

crontab -u user -l > tempfile
use whatever commands you want to update tempfile
contab -u user tempfile
rm tempfile

to make the changes take effect immediately without stopping and restarting cron . (As hicksdb8 mentioned, if you directly edit the user's crontab file in place not using crontab -e and not using crontab file , the changes might not be noticed by cron until the next time you reboot or stop and restart cron .)

sorry for the delayed response . here is the sample rows from the file we have

0-59 * * * * . /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh ca kreight v1 monitor >> /u/application/grpms/ca/logs/kreight/v1/startkreight.out 2>&1
 
0-59 * * * * . /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh us kreight v2 monitor >> /u/application/grpms/us/logs/kreight/v2/startkreight.out 2>&1

The above 2 rows one for Canada and other for USA. In this case I need to comment out USA and enable the crontab and also we have done all the migrations we need to uncomment the same . We have more than 250 rows in the file . How would this can be achieved using a script .

It can be solved in hundreds of ways based on the code snippets that I have already shown you. If you won't tell us how you plan to tell your script which country code(s) are to be enabled and/or which country codes are to be disabled, I am not going to attempt to enumerate hundreds of ways that I might imagine doing what you seem to want to do. Everything you should need to do this on your own was given to you (with two different approaches) in post #9 in this thread.

Until you clearly describe how you plan to interact with your script, I'm not willing to waste any more time trying to help you. Please answer the questions that have been asked in this thread and, then, we might be able to help you.

Note also that in your earlier posts country codes were shown in upper-case letters, but in post #10 the country codes turned out to be lower-case letters. You need to clearly specify how country codes and/or names will be given to your script and whether the country codes in your crontab file are upper-case, lower-case, or mixed-case.

2 Likes

Apologies for my wrong file entries provided earlier . Following is the sample file with all the countries
ca-> Canada
us-> usa
mx -> mexico
gb-> Great Britain
cn -> China
ge -> germany

0-59 * * * * . /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh ca kreight v1 monitor >> /u/application/grpms/ca/logs/kreight/v1/startkreight.out 2>&1
0-59 * * * * . /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh us kreight v1 monitor >> /u/application/grpms/us/logs/kreight/v1/startkreight.out 2>&1
0-59 * * * * . /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh mx kreight v1 monitor >> /u/application/grpms/mx/logs/kreight/v1/startkreight.out 2>&1
0-59 * * * * . /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh gb kreight v1 monitor >> /u/application/grpms/gb/logs/kreight/v1/startkreight.out 2>&1
0-59 * * * * . /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh cn kreight v1 monitor >> /u/application/grpms/cn/logs/kreight/v1/startkreight.out 2>&1
0-59 * * * * . /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh ge kreight v1 monitor >> /u/application/grpms/ge/logs/kreight/v1/startkreight.out 2>&1

We need to disable(comment out) country USA & enable all of the other countries when we are making application changes for USA. Once USA application chnages are completed, then we are planning to uncomment the cron entry for USA and once everything looks fine , we will go for the next country application changes . The only thing we need to see here , one country entries will be commented at a given time .

Let me if any further information required .

Thanks
Ron T

How about one static crontab entry that runs a script?

0-59 * * * * sh /path/to/script

And the /path/to/script has the commands

. /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh ca kreight v1 monitor >> /u/application/grpms/ca/logs/kreight/v1/startkreight.out 2>&1
. /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh us kreight v1 monitor >> /u/application/grpms/us/logs/kreight/v1/startkreight.out 2>&1
. /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh mx kreight v1 monitor >> /u/application/grpms/mx/logs/kreight/v1/startkreight.out 2>&1
. /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh gb kreight v1 monitor >> /u/application/grpms/gb/logs/kreight/v1/startkreight.out 2>&1
. /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh cn kreight v1 monitor >> /u/application/grpms/cn/logs/kreight/v1/startkreight.out 2>&1
. /u/data/environment;export ENV=prod;/u/application/pkms/startStoppry.sh ge kreight v1 monitor >> /u/application/grpms/ge/logs/kreight/v1/startkreight.out 2>&1

And now you only need a command to change the script file.

1 Like

The script file could even be parameter driven / controlled...

Slight difference with cron running a script and having x cron jobs is that the latter will run all the jobs simultaneously while the former has them being run sequentially. My guess is that these are fairly low resource jobs and will complete very quickly, so the distinction may be minimal.

Here is an idea for the contents of this script file.

/path/to/script

SKIP_COUNTRIES="gb mx"
ALL_COUNTRIES="us mx gb cn ge"

. /u/data/environment
export ENV=prod

for COUNTRY in ${ALL_COUNTRIES}
do
    for SKIP in ${SKIP_COUNTRIES}
    do
        [ ${COUNTRY} = "${SKIP}" ] && continue 2
    done
    /u/application/pkms/startStoppry.sh ${COUNTRY} kreight v1 monitor >> /u/application/grpms/${COUNTRY}/logs/kreight/v1/startkreight.out 2>&1
done

You can then either use vi to update the SKIP_COUNTRIES setting, or write an automated update using sed (or whatever) SKIP_COUNTRIES could even be read from a file, you have many options here...

At least for AIX it is easy: do the changes directly to the crontab (they are located in /var/spool/cron/crontab/<username> ), find out crons PID and send a kill -1 <PID> . That will make it reread its configuration (as any UNIX program should act upon receiving signal 1).

If this is too fancy for your liking here is a more brutal approach: you can just kill cron . As it is started from /etc/inittab with the restart clause the system will restart it for you once it doesn't run any more.

I hope this helps.

bakunin