大网数据广告活动
服务器运维问答

apt-get purge doesn't work with cron

· 52CE 我爱测

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…

问题原文

原帖链接 · jmreicha · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · Rabbit · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-04-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。