Crontab not executing bash script

落花浮王杯 提交于 2019-12-03 16:41:23

Note: These comments refer to /etc/crontab.

Before doing anything else, which cron are you accessing crontab -e or

su -vim
<your-favorite-editor> /etc/crontab

If you are using crontab -e, then no user field exists in that form of crontab. That might be why you're not running.

In your example, your user field is *. I would make it root or a user that has proper permissions.

Before running this program, I would make a dummy crontab entry that just does echo "Hello" and runs every minute. Get that to work on which ever crontab you're editing (crontab -e or vim /etc/crontab). Then using that as a template, get your script to run.

Next, see if cron is running:

ps -ef | grep cron

If it is not running, become root and start it by enter

/etc/init.d/cron start (Ubuntu and Red Hat).

You already have a good answer suggesting you add root as the user because of a permissions problem. I'm going to suggest more things to help you debug. I have run into a lot of cron problems over the years.

1) Set the email to a known address, unless you will continually monitor root's email

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/local/bin:/usr/bin
MAILTO=fred@somewhere.com
HOME=/

2) Until everything runs properly, take out the >/dev/null 2>&1 out of your cron entry, so you see the outputs in your email generated after the script runs.

3) Bump */15 down to an interval greater than it takes your script to run -- likr */5, so the script runs more often.

4) I do not know the exact reason, but scripts I run out of cron have to set up their own environments despite being run as that user in cron. This may include steps like cd /home/script-owner and running source .bashrc and calling other script(s) that set environment variables.

Remove the .sh extension from the script in /etc/cron.d and it will be called.

run-parts ignores files with a period in the name, so the .sh extension is preventing your script from running.

From man cron -

Files must conform to the same naming convention as used by run-parts(8): they must consist solely of upper- and lower-case letters, digits, underscores, and hyphens.

 */15 * * * * root /etc/cron.d/clear-mixtape-dir.sh >/dev/null 2>&1

Add user root because your permission seems to be only for root.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!