1

I am new to linux and I am trying to automate my update/upgrade script for my raspberry pi. Right now it is set to do it every 1 minute, so I can see whether it logs what it does. Later on I will be making it so that it would run once every X time. However it does not log anything nor does it create the out.log file.

The steps I take:

crontab -e
#I used the following line
01 * * * * root apt-get update -y && apt-get upgrade -y > /home/cronbin/out.log

I then save it:

CTRL + X
Y
ENTER

I did read about unattended upgrades and this post (apt-get upgrade not installing upgrades through crontab job) was also an interesting read, however I am trying to learn/work with linux and therefore decided to still ask this question in the hopes of finding out what I am doing wrong and how to make it work.

8
  • 1
    You need to add this to root's crontab, not your own. Dec 24, 2021 at 23:50
  • Your crontab entry should go to /etc/crontab as it contains the user column.
    – annahri
    Dec 24, 2021 at 23:52
  • @ajgringo619 I'm in the root's crontab if I enter in through ~, am I not? Dec 25, 2021 at 1:22
  • 2
    No one pointed this out, but the sixth column in your cron job (“root”) is not necessary and in fact wrong when using “crontab -e”.
    – jsbillings
    Dec 25, 2021 at 2:47
  • 1
    to clarify: to run every minute you need * * * * *, the above 1 * * * * runs ever hour at minute 1, so 8:01AM, 9:01AM.. etc
    – Ron
    Dec 25, 2021 at 7:31

3 Answers 3

1

apt generally requires root permissions, and so you should put this in the root crontab instead of a user crontab. On RPi, you can actually get away with using sudo in a user crontab as long as the user is pi, but this may not work on many other distributions - it's not a good practice.

Instead, use this:

sudo crontab -e 

This will open the root crontab for editing, and you can enter something like this which will run apt-get update and apt-get upgrade once per day at 12:00 noon, and write all of the results to the file /home/pi/upgrade_results.log:

* 12 * * * apt-get update && sudo apt-get -y upgrade >> /home/pi/upgrade_results.log 2>&1
  • Please review man apt-get to get the latest information for your system, and all of the important details re use of this command.

  • No -y option is needed for update as it does not generally prompt the user.

  • The >> redirect appends output to the log file; if you want only the results of the most recent run, replace the append redirect with the overwrite redirect: >

  • The 2>&1 redirect combines the normal stdout (1) output with any stderr (2) error messages; this is the same output you would get in your terminal if you ran these commands from the command line of your interactive shell.

  • see the crontab guru for help structuring your schedule.

When you test this cron job, you should not run it at 1 minute intervals because that may not be sufficient time to run both commands to completion. IOW, you're running these commands "on top of each other" with potentially harmful effect. If you feel the need to run this repeatedly for testing purposes, you should run the commands "manually" first, and then use the -s or --dry-run option in your cron job. Once you are satisfied the cron job is working, be sure to remove the -s or --dry-run option from the apt-get command.

upgrade can generate a fair amount of output, and if you use the append redirect >>, your log file will become large over time. You should review it regularly, and either trim or simply delete the file. If you want to constrain the size of your log automatically, consider the logrotate command, or perhaps another cron job that pipes the log file through tail with the -c option to a temporary location & overwrites the log file with the tmp file.

3
  • may I ask what I was doing wrong, that your code did work and mine didn't? Wasn't I giving root permission? (I thought the "root" command stood for that?) Dec 25, 2021 at 18:45
  • @ItrysohardbutIcryharder: AFAIK, there is no command called root. root refers to the user root - it's not a command. root is a.k.a. the "super user" - the user with the highest privileges on the system. Most Unix & Linux systems today use sudo to grant elevated privileges to 'regular' users. On the Raspberry Pi, the user pi is effectively the default root user; this because pi has been delegated ALL root privileges with no requirement to submit a password to use those privileges. There are good man pages on your system that explain sudo.
    – Seamus
    Dec 25, 2021 at 22:09
  • ah, thanks for clarifying! Appreciate the help Dec 26, 2021 at 1:04
1

You can run the upgrade in a subshell in crontab -evia the root user !Important

like so:

1 * * * * (/usr/bin/apt-get update;/usr/bin/apt-get dist-upgrade -y) >> /home/cronbin/out.log 2>&1

2>&1 will forward any errors to the output

which outout will append to /home/cronbin/out.log (although I think /var/log/cron.log is more appropriate place for the log)


also in crontab -e you do not specify the user after the time:

01 * * * * root
           ^^^^

you specify the user only in scripts that are within /etc/cron.*/ folders

4
  • Construction 2>&1 >> /home/cronbin/out.log do not do what you want. >> /home/cronbin/out.log 2>&1 do it Dec 25, 2021 at 8:29
  • 1
    good catch. I updated my answer
    – Ron
    Dec 25, 2021 at 17:35
  • 2
    Don't write to /var/log/cron.log directly. Either use the logging subsystem (see man logger and the logger command) or write to your own file in /var/log. Dec 25, 2021 at 17:56
  • I was just saying that that place is more appropriate. Of course the file can be called something custom..
    – Ron
    Dec 25, 2021 at 18:11
0

If you want to use command crontab -e to edit your cron record you should use line like this:

1 * * * * apt-get update -y && apt-get upgrade -y > /home/cronbin/out.log

When you use private cron record you should not add the username (root) P.S. And will be better to append the output to the log file instead of recreate it every time. Replace this:

> /home/cronbin/out.log

with this:

>> /home/cronbin/out.log

And also it is not wise to run it every minute because execution of update can take longer. And you will have two (or more) apt processes. Reasonable is to run update/upgrade once per 24 hours.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .