Crontab not executing bash script

2019-03-31 03:33发布

I very very rarely use Linux and so don't have any experience with bash scripts and cron jobs. This is in fact my first attempt. So it's probably something really simple to fix.

I have the following:

/etc/cron.d/clear-mixtape-dir.sh permissions are: 644

#!/bin/bash
# Clears the /tmp/mixtape2 directory
rm -rf "/tmp/mixtape2/"*

My crontab file looks like so:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

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

I'm trying to execute the .sh script every 15 minutes.

Everything i've found says this should work, but it doesn't.

Does anything like file permissions (on files within /tmp/mixtape2/) matter in this case? Or perhaps the permissions set on the actual .sh script - maybe they need setting to executable?

Any advice appreciated.

3条回答
萌系小妹纸
2楼-- · 2019-03-31 03:57
 */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.

查看更多
唯我独甜
3楼-- · 2019-03-31 04:05

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.

查看更多
干净又极端
4楼-- · 2019-03-31 04:14

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.

查看更多
登录 后发表回答