简体   繁体   English

sh文件没有在cron ubuntu上运行

[英]sh file not running on cron ubuntu

I am trying to run a shell script on crontab on Ubuntu platform. 我试图在Ubuntu平台上的crontab上运行shell脚本。 I have tried googling and other links but nothing has helped so far. 我尝试过谷歌搜索和其他链接,但到目前为止没有任何帮助。

This is my crontab : 这是我的crontab

*/2 * * * *  sudo bash /data/html/mysite/site_cleanup.sh

This is the content of my sh file: 这是我sh文件的内容:

#!/bin/sh
# How many days retention do we want ?
DAYS=0

# geting present day 
now=$(date +"%m_%d_%Y")

# Where is the base directory
BASEDIR=/data/html/mysite

#where is the backup directory
BKPDIR=/data/html/backup

# Where is the log file
LOGFILE=$BKPDIR/log/mysite.log

# add to tar 
tar -cvzf $now.tar.gz $BASEDIR

mv $now.tar.gz $BKPDIR

# REMOVE OLD FILES
echo `date` Purge Started >> $LOGFILE
find $BASEDIR -mtime +$DAYS | xargs rm
echo `date` Purge Completed >> $LOGFILE

The same script runs from a terminal and gives the desired result. 相同的脚本从终端运行并提供所需的结果。

Generic troubleshooting for noninteractive shell scripts 非交互式shell脚本的通用故障排除

Put set -x; exec 2>/path/to/logfile set -x; exec 2>/path/to/logfile set -x; exec 2>/path/to/logfile at the top of your script to log all subsequent commands to a file as they're run. 在脚本顶部set -x; exec 2>/path/to/logfile ,以便在文件运行时将所有后续命令记录到文件中。 If this doesn't work, you'll know that your script isn't being run at all; 如果这不起作用,您将知道您的脚本根本没有运行; if it does, you'll know where it fails and how. 如果确实如此,你会知道它失败的地方和方式。

If this is a personal crontab 如果这是个人的crontab

If you're running crontab -e as a user (without sudo ), then the crontab being modified is one for commands run with that user's permissions. 如果您以用户身份运行crontab -e (没有sudo ),则正在修改的crontab是用于使用该用户的权限运行的命令。 Check that file permissions allow that user to modify the content in question (which, if these files are in a cgi-bin directory, may require being run by the same user as the web server). 检查文件权限是否允许该用户修改相关内容(如果这些文件位于cgi-bin目录中,则可能需要由与Web服务器相同的用户运行)。

If your intent is to have commands run as root, rather than as your own user, be sure you use sudo when editing the crontab to edit the system crontab instead (but please take care as to your script's correctness in this case -- carelessness such as missing quotes or lack of appropriate precautions in xargs usage can cause a script to delete the wrong files if malicious filenames are created): 如果您的目的是让命令以root用户身份运行,而不是以自己的用户身份运行,请确保在编辑crontab时使用sudo来编辑系统crontab(但请注意脚本在这种情况下的正确性 - 粗心如此因为xargs使用中缺少引号或缺少适当的预防措施会导致脚本在创建恶意文件名时删除错误的文件:

sudo crontab -e             ## to edit the system (root) crontab

...or, if you're cleaning up files owned by the apache user (for example; check which account is correct for your own operating system and web server): ...或者,如果您正在清理apache用户拥有的文件(例如,检查哪个帐户对您自己的操作系统和Web服务器是正确的):

sudo -u apache crontab -e   ## to edit the apache user's crontab

Troubleshooting for a system crontab 系统crontab的故障排除

Do not attempt to put a sudo command within the commands run by cron ; 不要尝试cron运行的命令中放置sudo命令; with sudo 's default configuration, it requires a TTY (a keyboard and screen) to be attached to a session in order to run. 使用sudo的默认配置,它需要将TTY(键盘和屏幕)附加到会话才能运行。 Thus, your crontab line should not contain sudo , but instead should look like the following: 因此,您的crontab行不应包含sudo ,而应如下所示:

*/2 * * * *  bash /data/html/mysite/site_cleanup.sh

Your issue is likely coming from the sudo call from your user level cron. 您的问题可能来自您的用户级cron的sudo调用。 Unless you've gone through and edited the bashrc profile to allow that script to run without sudo it'll hang up every time. 除非你已经完成并编辑了bashrc配置文件以允许该脚本在没有sudo的情况下运行,否则它每次都会挂断。

So you can lookup how to run a script with no password by modifying the bashrc profile, remove the sudo call if you aren't doing something in your script that calls for Super User permissions, or as a last ditch, extremely bad idea you can call your script from root's cron by doing sudo crontab -e or sudo env EDITOR=nano crontab -e if you prefer nano as your editor. 因此,您可以通过修改bashrc配置文件来查找如何运行没有密码的脚本,如果您没有在脚本中执行要求超级用户权限的操作,则可以删除sudo调用,或者作为最后一个沟槽,非常糟糕的主意,您可以如果您更喜欢nano作为编辑器,请通过执行sudo crontab -esudo env EDITOR=nano crontab -e从root的cron调用脚本。

try to add this line to the crontab of user root and without the sudo. 尝试将此行添加到用户root的crontab而不使用sudo。 like this: 像这样:

*/2 * * * * bash /data/html/mysite/site_cleanup.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM