2

I have set my crontab to run 2 script:

$ crontab -l
0 20 * * * /usr/bin/bkp.sh --silent > /home/perforce/logs/bkp.log 2>&1
0 21 * * * /usr/bin/flsbk.sh --silent > /home/perforce/logs/flsbk.log 2>&1 

The first script is not running.

I set up on crontab -e not sudo crontab -e

The error I have is this. From /home/perforce/output.log:

====================================================================================

Starting Backup

====================================================================================

Output location:  /home/perforce/output.log

Running p4 verify...
/usr/bin/bkp.sh: line 22: p4: command not found
Done! (No error)

Running p4 admin checkpoint...
/usr/bin/bkp.sh: line 46: p4: command not found
Done!
/usr/bin/bkp.sh: line 51: p4: command not found
 (Error)

And my script is this:

#!/bin/bash

echo
echo "===================================================================================="
echo "Starting Backup"
echo "===================================================================================="

# Defines output folder and log name
OUTPUT_FOLDER='/home/perforce/'
OUTPUT_FILE='output.log'

OUTPUT=$OUTPUT_FOLDER$OUTPUT_FILE

echo "Output location: " $OUTPUT

# Creates a temp file
TMPFILE=`mktemp`

echo -n "Running p4 verify..."

# Run the p4 and send the output to the temp file
p4 verify -q //... > $TMPFILE

echo -n " Done!"

# Check if it's empty (no error)

# Copy the content to another variable, so we don't mess up with the original output
RESULT=`cat $TMPFILE`

if [ "$RESULT" != "" ];
then
  echo " (Error, saving to log)"

  # Save error on output file
  `echo $RESULT > $OUTPUT`

  exit
fi

echo " (No error)"

echo -n "Running p4 admin checkpoint..."

# Run the p4 and send the output to the temp file
p4 admin checkpoint > $TMPFILE

echo -n "Done!"

# The file created by the last command
COUNTER=`p4 counter journal`

FILE=/perforce_depot/checkpoint.$COUNTER

# Check if the file was created
if [ -f "$FILE" ]
then
  echo " (No error)"
else
  echo " (Error)"
  exit
fi

echo  -n "Backing up ..."

# Finaly, let's create the backup
#`cp -r /perforce_depot  '/media/perforce/Seagate Backup Plus Drive/perforcebk'`
START=$(date +%D)

FOLDER_NAME=`echo $START | tr -s '/' | tr '/' '_'`
FOLDER_PATH='/media/perforce/BackupDrive/perforcebk'
BACKUP_PATH=$FOLDER_PATH/bkp_$FOLDER_NAME

mkdir -p $BACKUP_PATH

cp -r /perforce_depot $BACKUP_PATH

echo " Done!"

p4 is located into /usr/local/bin

Any one have any idea why i am getting that error? If I ssh to the machine and run manually sh bhp.sh it runs without a problem and any errors.

  • it is located here /usr/local/bin – Savio Da Silva May 17 '13 at 13:02
  • 1
    what happens if you change calls to p4 to /usr/local/bin/p4? – Vladimir May 17 '13 at 13:11
  • 2
    See http://askubuntu.com/a/23438/9016 – geirha May 17 '13 at 13:31
  • when i run manually i am in the usr/bin and i run by the command sh bkp.sh – Savio Da Silva May 17 '13 at 13:44
  • The first command of the script is to verify a data of perforce and teh second create a checkpoint of the data then it should copy all the data to the relevant directory – Savio Da Silva May 17 '13 at 13:46
  • please note that only the user "perforce" can run the command on the script.... if i login as myself to the ubunutu machine that will not allow me to run that command. the question would be? does crontab runs the scripts as root or as the user that is logged on? – Savio Da Silva May 17 '13 at 13:48
  • From dan08 below: "If you use crontab -e then the file will run as whatever user you were logged in as. sudo crontab -e would cause it to be run as root."

    Cron runs from crontab based on the owner of that crontab.

    – Argusvision May 17 '13 at 14:11
  • Also the user has to be allowed to run cron. Is "perforce" allowed to run cron jobs? – Argusvision May 17 '13 at 14:14
  • hum....Argusvision i think it is because i have 2 jobs on the crontab
    0 20 * * * /usr/bin/bkp.sh --silent > /home/perforce/logs/bkp.log 2>&1 0 21 * * * /usr/bin/flsbk.sh --silent > /home/perforce/logs/flsbk.log 2>&1 the second job runs fine but the first job i got these errors please note that the 2nd job consist only of a cp command
    – Savio Da Silva May 17 '13 at 14:16
  • Could you add the results of crontab -l ? – Argusvision May 17 '13 at 14:31
  • perforce@22cansSRV1:~$ crontab -l

    0 20 * * * /usr/bin/bkp.sh --silent > /home/perforce/logs/bkp.log 2>&1 0 21 * * * /usr/bin/flsbk.sh --silent > /home/perforce/logs/flsbk.log 2>&1

    – Savio Da Silva May 17 '13 at 14:35
  • Ok. Sorry I think we may have gotten off on a rabbit trail. Since, your /home/perforce/logs/bkp.log is listing errors the script is running. It just pukes at p4. Did you edit the script to point to /usr/local/bin/p4 as suggested by Vladimir? – Argusvision May 17 '13 at 14:51
  • Since the error states "command not found" try specifying the path to p4. /usr/local/bin/p4 or add p4 to /usr/bin – Dan May 17 '13 at 13:15
  • 2
    Use the full path to "p4" in your script. You can add a line early in the script "P4='/usr/local/bin/p4'" and call it later with $P4 – Panther May 17 '13 at 17:17
  • why if i navigate to /usr/bin/ and run this on terminal sh bkp.sh the script will run and on crontab it wont – Savio Da Silva May 17 '13 at 19:33
  • should it not be the same think? the crontab will envoke go to /usr/bin and run that command from there? – Savio Da Silva May 17 '13 at 19:34
  • Just an update i placed the script on teh /usr/local/bin but still no joy – Savio Da Silva May 18 '13 at 20:54
  • 2
    guys, please put the updates in the question, not buried in the comments! – Jorge Castro May 19 '13 at 18:38

1 Answers1

2

Cron scripts do not run in your 'normal' environment. The output clearly states:

 /usr/bin/bkp.sh: line 22: p4: command not found

And you said:

p4 is located into /usr/local/bin

This can mean just that $PATH does not include /usr/local/bin, that's all. So modify your cron script and add this after your shebang:

#!/bin/bash

set -e
PATH="/usr/local/bin:$PATH"

Note: set -e will make your script quit if some commands fail. It is not required to solve your specific problem, but it may solve a lot of headaches in the future. Consider using it in all your scripts.