0

i am trying to write a script that will download, enable and add task with cron and then add auto update and upgrade task to the machine.

what i have until now is

sudo apt install cron
sudo systemctl enable cron 

until here all good then i add (after a research, the following commands)

<(crontab -l) <(echo '50 19 * * * sudo apt update -y') | crontab -
<(crontab -l) <(echo '00 20 * * * sudo apt upgrade -y') | crontab -

and when i check the file crontab -l

i see that the script did write the task like it should, but its not runing (i tried to run an apt install every min to see if its working)

but when i write the this command 50 19 * * 3 root sudo apt update -y with nano on that file /etc/crontab it worked

i tried to add root permeation on crontab -e but still not working

any solution?

is there is a way to add text line with script to /etc/crontab ? ( i couldn't find a way on line)

thanks you all

tops
  • 3

2 Answers2

0

You can do this on one line by setting your editor to ed.

printf "a\n50 19 * * * sudo apt update -y\n.\nw\nQ\n" | EDITOR=ed crontab -e

Essentially, append a line to the file with your content, exit edit mode, write, and save.

doneal24
  • 5,059
0

On any normal system, cron should available. So the installation of cron and the enablement via systemctl should not be required. It, however, makes your script dependent on systemd while that dependency is not necessary.

Try this:

#!/bin/bash

USER=root FILE=input.file.cron

or get it from the command line arguments

crontab -u $USER -l | grep -v '#--$FILE:$USER' > /tmp/cr-$USER awk '{printf("%-100s #--$FILE:$USER\n", $$0) }' $FILE >> /tmp/cr-$USER crontab -u $USER /tmp/cr-$USER rm -f /tmp/cr-$USER

Ljm Dullaart
  • 4,643