0

so I have been seeing something really weird and perhaps simple? but not simple enough that I have not been able to figure out. I have written a simple bash script that I am able to run without issues normally, the issue comes when I schedule a cron job which then does not run my script.

The script is executable, the script can be executed manually without errors/issues. I tried adding it to the crontab with the following:

# crontab -l
  30 * * * * /usr/scripts/test_script.sh

I have also tried to add a file under /etc/cron.d/test_script and tried to specify the path and shell:

# cat /etc/cron.d/tes_script
PATH=/bin:/usr/bin:/sbin:/usr/sbin
SHELL=/bin/bash
30 * * * * /usr/scripts/test_script.sh

I have been trying to check the logs with tailf /var/log/syslog and I see the following:

Feb 7 10:53:01 CRON[29203]: (root) CMD (/usr/scripts/test_script.sh)

so it seems like it might be runnign but in reality is not, (it triggers an update process that can be seen on a web interface for the program).

is there anything that I might be missing? Here is the script

#!/bin/bash
DIST="ubuntu"
LOGS="/var/logs/test_script.log"
RECIPIENTS="[email protected]"
declare -a POCKET=("release" "security" "updates")

xenial()
{
SERIES="xenial"
for pocket in "${POCKET[@]}"
do
        bzip2 -d "$HOLDER"*.bz2
        sync-pocks "$pocket" "$SERIES" "$DIST"
        sleep 5m
done
return 0
}

precise()
{
SERIES="precise"
for pocket in "${POCKET[@]}"
do
        bzip2 -d "$HOLDER"*.bz2
        sync-pocks "$pocket" "$SERIES" "$DIST"
        sleep 5m
done
return 0
}

xenial &> "$LOGS" &&
precise &>> "$LOGS" ||
cat "$LOGS" | mailx -s "Sync" "$RECIPIENTS"

Could it possibly some syntax issue on the script? and if so, why would it run without issues when ran manually?

8
  • Did you checked the mailbox of the user running the cron schedule (root)? Is there any mail from the cron daemon?
    – andcoz
    Feb 7, 2017 at 16:25
  • Who are the user and group owners of the script? What user is crond running as? Make sure the user/group that crond executes as has the permissions to execute the file.
    – airfishey
    Feb 7, 2017 at 16:25
  • root is the owner of the script and it does have execute permissions. I have restarted the service but it seems on the same state Feb 7, 2017 at 16:38
  • Where does the variable HOLDER get a value? Feb 7, 2017 at 16:39
  • Does the script work if you run it by hand?
    – ivanivan
    Feb 7, 2017 at 17:22

3 Answers 3

0

These are some wild guesses based your code:

  1. In which directory are your "$HOLDER"*.bz2 files? I do not see any cd command in your script. Probably the script is executed in the wrong dir.
  2. $HOLDER variable is undefined.
  3. Is sync-pocks in you path?
2
  • I'm sorry I omitted in here the holders directory, but it is on my script I just verified it. HOLDER="/var/lib/lrepos/lists/ and also the sync-pocks is under /usr/bin Feb 7, 2017 at 16:48
  • So, edit you question and fix the script.
    – andcoz
    Feb 7, 2017 at 20:16
0

When cron runs it, it gets executes with the 'sh' shell, which won't support the &> syntax. It could have worked correctly with the SHELL syntax you set for the /etc/cron.d/tes_script, but files in /etc/cron.d need a different syntax (adding the username as a field), which you didn't use.

To fix it, set SHELL=/bin/bash in your own crontab.

0

Quite late, but I figured the solution: run the script with the -l option. the cronjob will not run because of the variables I had setup on my login shell, also it will not log any errors. As per the man page:

-l Make bash act as if it had been invoked as a login shell (see INVOCATION below).

so my crontab will look like this:

30 * * * * /usr/scripts/test_script.sh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .