0

I'm having trouble with setting a cron job to run daily when I enter the script to /etc/cron.daily when I put it there it doesn't run at all like it should be doing.

I setup a script so that it can keep my repository up-to-date and then download any new packages it finds that needs upgraded. I didn't want to do crontab -e because it doesn't let me set its own individual file to run this script because it only seems to dump it into /tmp directory. Here's what my cron script looks like;

#!/bin/sh

30 22 * * 0 apt-get update && apt-get upgrade -y

There doesn't appear to be any indication that this works, except for the fact I get errors relating to apt in mailx saying

E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root

I would of thought this ran at root level inside of /etc/cron.daily since it's owned by root? The permissions read as drwxr-xr-x 2 root root 4096 Sep 28 06:02 cron.daily I wasn't too sure if I should add sudo to the command because then it would just sit there waiting for a password to be entered.

also how would I be able to get it to log what packages updated so I can review them later? or does apt already log these to it's own log file somewhere? if so where is it? I would like it to just output the name and versions of the packages installed it upgraded from to display something like this into a log file, ie;

<package> @ <version> --> <new_package> @ <version>   <date> <time>
0

A few things...

  1. 30 22 * * 0 means every Sunday at 10:22pm. Not daily. Use a * in the fifth time field if you want every day of the week.

  2. Files in /etc/cron.d/ take a slightly different format. The second argument (after the time string) sets the user it will run as. To do things this way your file should read:

    30 22 * * *  root  apt-get update && apt-get upgrade -y
    
  3. Files in /etc/cron.daily/ are acually supposed to be standalone scripts, not cron-formatted strings. To do things this way, your file should read:

    #!/bin/sh
    set -e
    apt-get update
    apt-get upgrade -y
    
  4. And perhaps most importantly... if you want to automate updates, I'd suggest something like apticron. It gives you much better control over what's installed automatically and what isn't. It also sends out emails with changelogs, etc... I think it's what you want.

  • So what you're saying is I'd have put the cron job into /etc/cron.d pointing it to the script inside of /etc/cron.daily where the script resides for it to run daily. is that right? – user94959 Oct 5 '14 at 8:05
  • No they're different approaches and currently you're somewhere in the middle. Pick one, or better yet, pick apticron. – Oli Oct 5 '14 at 23:14

Your Answer

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