I'm seeing some strange behavior with a script getting executed by cron and I'm not really sure what's happening. The script just looks like it is going to remove the package but then just hangs at the end and never does anything.
Here's the cronjob.
06 15 * * * root /myscript.sh > /tmp/script.log 2&>1
Here's (a simplified version of) the script.
#!/bin/bash
PACKAGES_TO_PURGE="htop"
if [ -n "$PACKAGES_TO_PURGE" ]; then
echo "Purging $PACKAGES_TO_PURGE"
echo
#sudo apt-get purge -y $PACKAGES_TO_PURGE
apt-get purge -y $PACKAGES_TO_PURGE
echo "Done"
echo
else
echo "Nothing to purge"
fi
Here's the output of the log file.
0 upgraded, 0 newly installed, 1 to remove and 62 not upgraded.
After this operation, 188 kB disk space will be freed.
Done
Running this script by hand works, I only run into issues running it with cron so I'm not exactly sure what the deal is.
I can even get this script to work if I add a sudo
into the apt-get command.
I'm thinking there is something easy I'm overlooking but nothing is jumping out at me.
Have a look at the answer in this post; someone tried to make an
apt-get upgrade
from cron and got a similar issue.It seems that you need to setup the PATH environment variable in order to make apt work. Eg :
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
I ran into a similar issue trying to run my script in crontab. I was automating updates using apt-get update in my script. I need to add the path found with the command $PATH to my script under:
Afterwards my script worked with crontab.