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.
Add user
root
because your permission seems to be only for root.Note: These comments refer to /etc/crontab.
Before doing anything else, which cron are you accessing
crontab -e
orIf 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
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 runningsource .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
-