4

I created a bash script to automatically update my Ubuntu Server. Here is the script:

#!/bin/bash

apt-get update
apt-get upgrade
echo "Update am $(date "+%d. %m. %y|%H:%M:%S) >> /var/log/update.log
exit 0

The log file is always empty. crontab:

0 3 * * * /drives/files/scripts/update.sh

I know, that I can set, that the Ubuntu Server update automatically in the settings, but this is to learn about cron and a little bit scripting.

Thanks

  • 1
    What does not work with your script? Does it not run or what is wrong? – Uwe Plonus May 12 '15 at 6:48
1

I suppose that you are using crontab with normal user which means that he can't update/upgrade a system without providing a sudo.

Also apt-get update and apt-get upgrade will prompt user for confirmation so you have to pass this confirmation before.

yes | apt-get update
yes | apt-get upgrade

So to solve it you have to run your commands with sudo, but this will put you in problem how to provide the password to the sudo.

Moreover

echo  "Update am "$(date "+%Y-%m-%d %H:%M:%S") >> /var/log/update.log

is writing in /var/log/... which means need permission to do that which is not founded for a normal user.

In order to solve that you should run your script in the root cron.

sudo crontab -e

will open the root cron file in your default editor.

Add the entry of your script

0 3 * * * /drives/files/scripts/update.sh

Now save and exit.

PS: just in case, be sure that you give execution permission for your script. to do that run the command:

chmod +x /path-to-script
  • Ok, thanks for this fast answer. First: I use sudo. I open the crontab with sudo. But I think I know the problem. I forgot the "yes". I will correct it and then report. – ssd_rider May 12 '15 at 5:58
  • @ssd_rider you are welcome – Maythux May 12 '15 at 5:58
  • And I noticed too, that the script wasn't executable. Thanks. Is there a chance to test the cron, without to wait to the start time of the job? Like an testparm from samba? – ssd_rider May 12 '15 at 6:07
  • you can find your answer here stackoverflow.com/questions/8132355/test-run-cron-entry – Maythux May 12 '15 at 6:09
  • I didn't test the cron but i manuelly tested the script and I noticed, that the script also don't work perfectly. Now it have to work. I will wait to tomorrow. – ssd_rider May 12 '15 at 7:07
0

Why its not working ?

To do apt-get update and apt-get upgrade you must become a sudo user. I mean your script should executed with sudo privileges.

Any other stoppings ?

Yes , you are not doing giving -y that let upgrade command automatically download after update.

Is it a major thing ?

Yes , if you dont pass -y along with command then your cron will stop and looks for '-y' arguement and it wont execute till it get '-y'

How to execute cron script as root user ?

sudo crontab -e

It will ask you choose editor and choose nano then there add your line. But complete script should be this

apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y

Hope that helps.

  • Why -ve vote ? It means my answer wrong or too good ? – rɑːdʒɑ May 12 '15 at 8:08
  • Same thing happens to me and don't know what matters with people who downvote – Maythux May 12 '15 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.