2

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.

  • 1
    Try running your script via env. Specifically env -i /myscript.sh. Running it with am empty environment will probably give you a hint about the problems. – Zoredache Apr 9 '18 at 23:07
  • 1
    If the script hangs it's likely that it's in some part of the full script. Can you post the whole thing? Also, apt-get isn't going to run properly in any case because you haven't set PATH. Try set -o xtrace at the top of the file to see what happens in the logs. – l0b0 Apr 10 '18 at 1:25
1

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

0

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:

    #!/bin/bash
    PATH=/usr/lib64/ccache:usr/local/bin:/bin:/usr/local/sbin:/usr/sbin:home/learner2/.local/bin:/home/learner2/bin
    apt-get -y update

Afterwards my script worked with crontab.

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.