10

I'm kinda new in Linux and I've just learned about scheduling tasks with cron. I have this small shell script that I've been using manually till now:

sudo apt-get check && sudo apt-get update && sudo apt-get upgrade && sudo apt-get autoremove && sudo apt-get autoclean

Now I want to schedule it using cron so I won't manually do it every day, but it seems I can not do so. I suppose it is because it requires my sudo password in order to proceed? Is there any way I can make this work without auto-accepting the upgrades and so on?

1 Answers1

14

It's a better idea to use "unattended-upgrades" instead.

Its purpose is to keep the computer current with the latest security (and other) updates automatically. [1]

To install:

sudo apt install unattended-upgrades

Read more about how to get it work: here.


To address your question, you can edit /etc/crontab file and run your commands using root user without the need of using sudo in your own "crontab" file.

nano /etc/crontab

and add a line like:

45 21 * * * root apt-get update > /home/ravexina/out.log

which runs apt-get update using root user at "21:45" every night and logs the output to /home/ravexina/out.log.

Ravexina
  • 57,426
  • 1
    Thanks for your response, but it's for "learning" purpose, or at least this is how I see it. I want to learn why it doesn't work and what can I do in order to make it work. Can you please help me? – Alexandru Banu Jun 08 '17 at 22:06
  • Thanks! I tried to edit the /etc/crontab in order to make it work, but I still have some doubts it does. The script is placed in /usr/local/bin, under the name sysup. What I tried in crontab was

    15 21 * * * root sysup && echo "Succes!"

    Still, I had no such message displayed on my terminal. Any thoughts why? Not sure what I'm doing wrong. I was expecting that the output of the scheduled task still appear on terminal, prompting me when required input.

    – Alexandru Banu Jun 08 '17 at 22:24
  • 1
    First of all, you are not going to see the "success" so redirect it to a file, second use absolute paths in your script to make sure it's going to be work correctly. don't forget to make your script executable too: sudo chmod +x /usr/local/bin/sysup at the end you can put your commands directly in /etc/crontab to make sure thy really work. – Ravexina Jun 08 '17 at 22:27
  • Thanks, I will try so right now. Please don't mind if I send you any more message regarding this, but I hope it will work now! – Alexandru Banu Jun 08 '17 at 22:29