Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
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 askubuntu.com/a/23438/9016 –  geirha May 17 '13 at 13:31
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 –  bodhi.zazen May 17 '13 at 17:17
2  
guys, please put the updates in the question, not buried in the comments! –  Jorge Castro May 19 '13 at 18:38
show 13 more comments

2 Answers 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.

share|improve this answer
add comment

I think your script will work if you don't use the 'echo' statements. I came across a similar issue when running a couple of python scripts in crontab. I believe cron runs an instance of the terminal without support for IO to the screen. If you need to store any information which was being printed to the screen you should append it to a log file.

Hope that helps, Matt

share|improve this answer
    
The use of echo or not has nothing to do in this bash script. Actually, echo is one of the most informative non-intrusive commands of the bash. –  Braiam Sep 17 '13 at 12:06
    
Welcome to Ask Ubuntu Matthew! You're absolutely right, information-wise, but the cronjob is logging the output to a file; the cronjob itself does not contain an echo command. Those echo commands are in the script. –  Alaa Ali Sep 17 '13 at 13:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.