Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a bash script via Cron

Tags:

bash

cron

I have seen other questions that are similar but I can't find any real information on how to figure out the proper way to run a Bash script via Crontab. The .sh file is located in the user directory (in my case serverpilot).

The script is trying to copy the contents of the apps folder and send to my S3 bucket. The script works perfectly when run with sh backupS3.sh from the terminal but no joy via Cron. I've seen people reference PATH variables and the like but no idea where to even begin!

/backupS3.sh

#!/bin/sh
echo 'Started backing to S3'
date +'%a %b %e %H:%M:%S %Z %Y'
aws s3 sync /apps s3://bucketname
date +'%a %b %e %H:%M:%S %Z %Y'
echo 'Finished backing to S3'

crontab

*/10 * * * * /backupS3.sh >> backupS3.log  

Can anyone point me at anything obvious I'm doing wrong? Thanks!

EDIT: I've added 2>&1 on to the end of the cron command and now I am getting something in the log file:

/bin/sh: 1: /backupS3.sh: not found

like image 540
André Goldstein Avatar asked Jun 01 '16 16:06

André Goldstein


People also ask

How to run shell script as cron job in Linux?

This is a very common requirement by system administrators who wish to run a number of shell scripts on a regular basis, to automate various system related tasks. Here are the steps to run shell script as cron job. 1. Create Shell Script Open terminal and run the following command to create a blank shell script file e.g. backup.sh 2.

How do I run a bash script from a crontab?

you can do as cas suggests either by prefixing in crontab with /bin/bashinstead of sh, or by starting your script with a shebang line: #!/bin/bash- see if it behaves better once you definitely have it running under bash and not some other shell. Another possibility is that you aren't looking in the right place for the output file.

How do I set up a cron job?

Step 1: Give crontab privilege. Step 2: Create cron file. Step 3: Schedule your job. Step 4: Validate the cron job content. Script to create cron job using bash shell script. List the cron jobs. Advertisement. In my last article I had shared the steps to downgrade rpm to a specific old version in Linux and Unix.

Why does my shell script fail when launched from cron?

So a script executed in a user's shell works perfectly, but fails when launched from cron. The number one culprit in this case is that your shell's $PATH environment variable differs from that of cronjob's shell. To test run '/bin/echo $PATH > myShellPath'. In cron run '/bin/echo $PATH > myCronPath'.


1 Answers

I've had the same problem where log file was created empty and the only way to see what is going on was to add 2>&1 at the end of the cron job.

*/10 * * * * /home/users/me/backupS3.sh >> /home/users/me/backupS3.log 2>&1

If anybody wonders what 2>&1 actually mean here is a short explanation:

2 - refers to a file descriptor used to identify stderr

1 - refers to a file descriptor used to identify stdout

So 2>&1 basically translates to: Redirect stderrtostdout

like image 71
foobar Avatar answered Oct 18 '22 17:10

foobar