Rename inside the crontab file

Hi Team,

i have below file is my crontab,

00 * * * * /home/****/_20480.sh >> /$$$$/#####/status.txt
00 * * * * /home/$$$$$/_20480.sh >> /$$$$/#####/status.txt

i want to replace/rename the file _20480 with 40960

I have tried with below command but there is no change happening in
crontab file.

crontab -l -u splunk | sed 's/*_20480/40960/' | crontab -

please suggest

Try something like this:

# crontab -l -u hergp
0 * * * * /home/hergp/hello.sh
# EDITOR="sed -i s/hello/world/" crontab -e -u hergp
crontab: installing new crontab
# crontab -l -u hergp
0 * * * * /home/hergp/world.sh

First off, it is good that you are trying to use the crontab command to alter the file.

However, I get the idea that you have lots of redundant lines in your crontab file. Otherwise you would not bother with a script like this.

Correct me if I'm wrong, but otherwise you should simply use the crontab without pipes and sed. Just a single one line entry pointing to the shell script. Let it do all of checking testing and renaming. You can safely use most text tools on the script - you cannot do that with crontab files necessarily.

If I'm correct you should copy all of that crud out of crontab and place it in a single larger shell script where you won't have problems like the one one you have now. crontab is a special kind of file and it is easy to mess it up and get lots of errors generated into many email messages, for example.

Do not rename the crontab file.

The preferred way would be to use the crontab command to delete the job and then the crontab command to enter the new job.

You should not edit the crontab directly because changes will not be seen by the cron daemon (for a while). When the system boots cron daemon starts, reads all the crontabs into memory along with their last modified date/time, and then runs. Periodically, (typically once every 24 hours) cron will check by comparing the date/time of crontabs that it has the latest versions. If a crontab has changed it will then notice it and reload that table. Until then no changes will take place.

If you are desparate you could (but not recommended) edit the crontab file directly and then get someone with root access to stop/start or restart the cron daemon so that the change(s) are read in. How you restart the cron daemon will depend on your OS which you do not state.

Hope that helps.

A leading * requires a leading literal *

crontab -l | sed 's/_20480/40960/' | crontab -

The idea with a leading character and a trailing character is good, it prevents a partial match in the wrong line. The matching boarder charaters must be re-added in the substitution string

crontab -l | sed 's#/_20480\.#/40960.#' | crontab -

A compromise is a word match; the anchors \< and \> match a word boundary but not a character, are not to be re-added in the substitution string

crontab -l | sed 's/\<_20480\>/40960/' | crontab -

Hi Everyone,

Thanks for the update everyone, i had tried above commands , it did not work for me. I wanted to rename file name, putting in more details so that i can get my answer for my question:

i have below line in crontab

00 * * * * /home/@@@@/ulimit_nofile_status_20480.sh >> /export/####/$$$$$/ulimit_nofile_status.txt

00 * * * * /home/@@@@/ulimit_nproc_status_20480.sh >> /export/####/%%%%/ulimit_nproc_status.txt

i want to change the file name (ulimit_nofile_status_20480.sh and ulimit_nproc_status_20480.sh) to (ulimit_nofile_status_40960.sh and ulimit_nproc_status_40960.sh) these are file names.

I want to rename 20480 to 40960

Once again thanks team update, waiting for reply once again on the updated post

try what @hergp suggested with re sed;

sed -r 's/(ulimit_n\w+?_status_)20480.sh/\140960.sh/gI'

Hi hergp and abdulbadii,
The person who started this thread still hasn't told us what operating system is being used for this project. The standard crontab utility:

  1. does not have a -u option,
  2. uses $EDITOR to get the name of the editor to use to edit a crontab entry (not an editor command to be broken into a command name, options, and operands by a shell and then to be executed),
  3. uses basic regular expression as the RE type in a sed substitute command (meaning that on many systems unescaped parentheses and an escaped w have no special meaning), and
  4. there is no I flag on a sed substitute command.

The first two issues above apply to the suggestion hergp provided; all of the above issues apply to the suggestion abdulbadii provided.

I wouldn't be surprised if some systems ignore the standards on these issues and behave as you have indicated; but you might at least have specified the environment you were using that supports these extensions instead of implying that these will work on John009's unspecified system.

1 Like

Thanks team, was able to resolve the issue by the below command

ssh $line "crontab -l | sed 's/20480/40960/' | crontab -"