Crontab not executing bash script

30,611

Solution 1

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.

Solution 2

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
[email protected]
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.

Share:
30,611
Barry Jarvis
Author by

Barry Jarvis

Updated on September 18, 2020

Comments

  • Barry Jarvis
    Barry Jarvis over 3 years

    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.

    • Satya
      Satya about 11 years
      try changing permissions to 777
    • fedorqui
      fedorqui about 11 years
      Add /bin/bash in your line: */15 * * * * /bin/bash /etc/cron.d/clear-mixtape-dir.sh >/dev/null 2>&1
    • Gordon Davisson
      Gordon Davisson about 11 years
      @Satya: Permissions should almost never be set to 777. In this case, the permissions on the script should be 755, and the permissions on /tmp/mixtape2/ need to allow write to the user running the cronjob.
    • twalberg
      twalberg about 11 years
      Better yet, make the permissions 700 and make sure the owner is root. Unless you want it to run as a different user - then change the ownership appropriately, and still make it 700, then update your cron definition to run the script as that user.
    • Barry Jarvis
      Barry Jarvis about 11 years
      are we talking about the permissions of the files i need to delete or the permissions of just the 'mixtape2' dir? The problem with permissions is that every time a file is created (temp file) inside the mixtape2 dir is it created as 644. so i don't want to have to manually change the permissions everytime i want to clear them. I guess i could add a chmod to my bash script?
  • Keith Thompson
    Keith Thompson about 11 years
    There are two different forms of crontab. In a system crontab, a line consists of 5 fields for the time, one for the name of the account that will run the job, and the rest for the command. In a normal crontab, the kind manipulated with the crontab command, the user name is not specified -- and if you do specify it, it will be taken as part of the command name. (The job will executed by whatever account ran the crontab command.) Commands to be run by root can be installed either as system crontabs or by running the crontab command from the root account.
  • Barry Jarvis
    Barry Jarvis about 11 years
    thanks for the help... I've added root, also i've added email address and removed the >/dev/null 2>&1 but still no luck. I'm getting no emails through at all. So it doesn't look like the crontab is even actioning? Any ideas on this one? One thing i haven't done is your step 4... as quite frankly i'm too new to all this to know what the hell any of that means ;)
  • fedorqui
    fedorqui about 11 years
    This is such a comprehensive explanation, @octopusgrabbus. Should be kept somewhere like stackoverflow.com/tags/crontab/info to be accessible for users trying to debug their crontab!
  • octopusgrabbus
    octopusgrabbus about 11 years
    A simple cron script -- like echo "Hello" -- would at least clear up your format problems. From there, you can get into any script-related problems. @BarryJarvis