Having multiple shell scripts in cron

Issues related to applications and software problems and general support
Post Reply
afernandezody
Posts: 50
Joined: 2019/06/25 13:10:33

Having multiple shell scripts in cron

Post by afernandezody » 2021/06/22 22:34:25

Hello,
I'm trying to add 2 shell scripts to cron. If I use the following command to add the 1st script:

Code: Select all

sudo echo "*/1 * * * * /home/centos/fecha.sh" | sudo crontab -u centos -
However, when I try adding a second script,

Code: Select all

sudo echo "*/3 * * * * /home/centos/fecha3.sh" | sudo crontab -u centos -
crontab will erase the previous one. From some posts, a way to to overcome this is to write them to a temporary file. The command would read:

Code: Select all

sudo (crontab -l -u centos; echo "*/1 * * * * /home/centos/fecha.sh") | crontab -u centos -
However, this results in the error message

Code: Select all

-bash: syntax error near unexpected token `crontab'   
Any suggestions on how to add 2 shell scripts to cron? Thanks.

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Having multiple shell scripts in cron

Post by jlehtone » 2021/06/23 05:57:55

Why do you "sudo echo"? Every account can 'echo'; no sudo required.

What is this account 'centos'? Why that account has a crontab, rather than your own account?
man 1 crontab wrote:-u
Append the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines "your" crontab, i.e., the crontab of the person executing the command. Note that su(8) can confuse crontab and that if you are running inside of su(8) you should always use the -u option for safety's sake. The first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename "-" is given.

-e
This option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified crontab will be installed automatically.
I would edit interactively:

Code: Select all

sudo crontab -u centos -e
Note that it is possible to echo multiline string:

Code: Select all

echo -e "*/1 * * * * /home/centos/fecha.sh\n*/3 * * * * /home/centos/fecha3.sh" | sudo crontab -u centos -

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Having multiple shell scripts in cron

Post by TrevorH » 2021/06/23 08:20:33

What's with all the sudo commands? Most likely you are either root in which case it isn't needed as it can do whatever it needs to do without sudo or you are the user in question and therefore don't need sudo since you are the owner of the crontab in question anyway.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

afernandezody
Posts: 50
Joined: 2019/06/25 13:10:33

Re: Having multiple shell scripts in cron

Post by afernandezody » 2021/06/23 12:31:47

Hi @TrevorH & jlehtone,
The sudos are necessary because the commands are run by chef (not by a particular user and therefore crontab cannot be edited interactively). Everything else in the chef script was working fine so I was trying to isolate & figure out how to fix the cron thing that wasn't functioning properly. Yesterday I tried the multiline approach but didn't work for me, maybe it was because I didn't include "-e" when invoking the echo command (have to recheck). I also made some progress flipping the order of the echo and crontab -l commands. Thanks.

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Having multiple shell scripts in cron

Post by jlehtone » 2021/06/23 12:58:57

Doesn't chef have recipes/resources for editing crontab entries?
https://docs.chef.io/resources/cron/

afernandezody
Posts: 50
Joined: 2019/06/25 13:10:33

Re: Having multiple shell scripts in cron

Post by afernandezody » 2021/06/23 13:08:36

I'm not digging that deep. Chef is calling a bash script but it runs the commands as sudo, that was my point.

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Having multiple shell scripts in cron

Post by jlehtone » 2021/06/23 14:25:49

afernandezody wrote:
2021/06/23 13:08:36
I'm not digging that deep.
But, should you? Aren't recipes more self-documenting (and easier to get right) than cryptic oneliners?

Post Reply