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
/usr/bin/bkp.sh: line 51: p4: command not found

And my script is this:


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

# Defines output folder and log name


echo "Output location: " $OUTPUT

# Creates a temp file

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

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

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


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`


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

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 '/' '_'`

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 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 – Panther 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

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:


set -e

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.


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

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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