How to run a bash script via Cron

crontab bash script
run bash script on cron job
crontab run bash script every minute
crontab run shell script
cron run bash script as root
crontab bash script not running
crontab unable to execute shell script
run script cron linux

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

In pathnames, the leading / doesn't mean "home directory"; it always refers to the root of the file system, no matter who you're logged in as. You need to use the actual full, absolute path to the script (e.g. /home/serverpilot/backupS3.sh). If the crontab belongs to the same user whose home directory holds the script, you can use "$HOME"/backupS3.sh and the system will fill in their home directory path for you. Or, even more simply, just use ./backupS3.sh, since cron jobs start with their working directory equal to their owner's home directory.

If that doesn't work, then either what you mean by "user directory" is not the same as the POSIX concept of "home directory", or the script is not executable (which you can fix by running the command chmod +x backupS3.sh once).

If you're not sure what the full path is, just run the pwd command while you're in the same directory that holds the script, and put the output, followed by a slash, in front of the script name.

How to run a bash script via Cron, is executable and has the right permissions. Script to create cron job using bash shell script. Let us put them all together in a script #!/bin/bash if [ `id -u` -ne 0 ]; then echo "This script can be executed only as root, Exiting.."

First, change #!/bin/sh to #!/bin/bash, next use the full path to the script (also .sh isn't needed in the file name)

*/10 * * * * /home/users/me/backupS3.sh >> /home/users/me/backupS3.log  

Running bash inside cronjob, If the crontab belongs to the same user whose home directory holds the script, you can use "$HOME"/backupS3.sh and the system will fill in their home directory path for you. Or, even more simply, just use ./backupS3.sh , since cron jobs start with their working directory equal to their owner's home directory. Any script put into these directories will run once per hour, once per day, once per week, or once per month. These scripts will run as the root user, without any special flags. Cron also allows for scripts to be run by users other than root.

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

Bash script not executing from crontab, if you want to run a bash script then use bash, not sh. you can do as cas suggests either by prefixing in crontab with /bin/bash instead 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. the script is working without cron :) i will try this now, i mean when i run the script ./vpn-check.sh is checks and reconnect the vpn , when is on cron it doesn't reconnect it – user3101956 Dec 14 '13 at 10:57

How to execute shell script via crontab?, You need to give your cron a PATH . For instance: SHELL=/bin/sh PATH=/bin:/ sbin:/usr/bin:/usr/sbin. In your case, try putting this before your� I have script using #!/bin/bash as header ; I put the script on /home/wc3/palert/ analysis: crontab can't run my script ; But if I run manually it show the output and also I can see the result on my web ; It means crontab can't get the environment not like when you run your script on your script folder ; answer:

How to use cron in Linux, Your script is missing a #! line at the start, which is the magic interpreted by the kernel to say which command interpreter is to be used for the script. Make it look � Run via sh: $ sh script.bash hi General advice. It's generally advisable to use the exact shell you're expecting with your shebang. If you suspect that you're running into problems where you suspect dash or some other shell is being used, you can always make your shebang explicit by changing it from #!/bin/sh as your shebang #!/bin/bash.

Automate running a script using crontab, Using the crontab command not only allows you to edit the command, This line runs my self-written Bash shell script, rsbu, that backs up all� Running a script using the cron service, that executes normally from the shell but does not exhibit the same behavior when running from crontab. Root Cause One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user’s shell environment.

Comments
  • You say it's not working, but what's actually happening?
  • Sorry @JordonPhillips the script isn't running I believe as the sync never happens and the echo statements aren't printed to the log
  • I should point out that it is creating the log file but that the log file remains empty
  • Your script is a sh script, but running it with bash is harmless. The opposite generally does not hold. Either way, you should understand the difference before it jumps up and bites you. See e.g. stackoverflow.com/questions/5725296/…
  • Also, this isn't really a programming question; serverfault would be a better choice in the future.
  • Thanks, where would I run that chmod command? As a one-off in the terminal?
  • Yes, it just has to be run once.
  • Many thanks. Incidentally the aws s3 sync /apps s3://bucketname line within the script itself, will that know where to look or will that need to be the full server path also? Thanks again.
  • If the directory is /apps, great. If it's apps under the user's home directory, then that also needs to be "$HOME"/apps.
  • When Cron runs a job, it starts in your home driectory. Maybe all you need is ./backupS3.sh
  • Thank you. When you say use the fullpath, I'm not sure where to point that to? I've added the Crontab to the serverpilot user (I also have access to the root of the server but thought it was preferable to login as serverpilot). I know the full server path to it, should I use that?
  • The bash hint is a red herring; your script currently does not contain any Bash-specific constructs.
  • Thanks for the info @triplee
  • @tripleee red herring? It's additional and constructive since the user is posting this as a "bash question" No need for a down vote, as the path was the issue.
  • In fact, I read the entire answer, and it would be fine if you removed the part which is incorrect. There are many situations where that advice would help, but this isn't one of them. Recommending it anyway looks like cargo cult programming to me, which I do think is based on poor principles (incidentally, not "principals").