Crontab does not execute bash script

I very rarely use Linux and therefore have no experience with bash and cron scripts. This is actually my first attempt. So maybe something is really simple to fix.

I have the following:

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

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

My crontab file looks like this:

 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 try to run a .sh script every 15 minutes.

Everything I found says this should work, but it is not.

In this case, are any file permissions important (in files inside / tmp / mixtape 2 /)? Or maybe the permissions set on the actual .sh script - maybe they need to install the executable?

Any advice is appreciated.

+4
source share
3 answers

Note. These comments apply to / etc / crontab.

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

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

If you use crontab -e, then there will be no user field in this form of crontab. Perhaps that is why you are not working.

In your example, your user field is *. I would make it root or a user who has the appropriate permissions.

Before starting this program, I will make a fictitious record of crontab, which simply makes echo "Hello" and starts every minute. Get this to work on which ever crontab you are editing (crontab -e or vim / etc / crontab). Then, using this as a template, run your script.

Next, see if cron works:

ps -ef | grep cron

If it is not running, run it and enter it

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

You already have a good answer involving adding root as a user due to a permission issue. I am going to offer more things to help you debug. Over the years, I have encountered many cron issues.

1) Set the email address to a known address if you do not constantly monitor the root email address

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

2) Until everything is done correctly, remove >/dev/null 2>&1 from your cron entry, so that you will see the outputs in your letter created after the script was run.

3) Bump */15 accurate to an interval exceeding your script to run - likr */5 , so the script runs more often.

4) I don’t know the exact reason, but the scripts that end with cron should create their own environments, despite the fact that they are run as a user in cron. This may include steps such as cd /home/script-owner and starting source .bashrc , as well as calling other script (s) that set the environment variables.

+3
source

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

run-parts ignores files with a name period, so the .sh extension prevents the script from running.

From man cron -

Files must comply with the same naming convention that is used in execution parts (8): they must consist only of upper and lower case letters, numbers, underscores, and hyphens.

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

Add the root because your permission is only for root.

-1
source

All Articles