Script doesn't execute in crontab - bash

I wanna run a script using crontab in every minute. The script is located at /Users/robin47/Desktop/script/demo.sh. As a side note, I give the execute permission of demo.sh file with chmod +x demo.sh command.
But it doesn't execute the script. Here is the process I'm following:
Run crontab -e from the terminal.
robin47#mac-pro ~ % crontab -e
this open a file(crontab.xxxxxxx) in nano which is located under tmp folder, as an example File: /tmp/crontab.V4DcXEJU8g.
This is my crontab.xxxxxxx file:
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /Users/robin47/Desktop/script/demo.sh
Get from here, this doesn't execute my demo.sh script.
If I run sh command from terminal like sh demo.sh, it works. And if I directly echo(or run any command like touch, mkdir) in crontab like this
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * echo 'hello' >> /Users/robin47/Desktop/script/text.txt
It also works. But doesn't run the script using crontab. I appreciate your help, Thanks.
This is my demo.sh file:
#!/bin/sh
echo "hello world" >> /Users/robin47/Desktop/script/text.txt

refer below site for cron entries , also check your script have executable permission
chmod oug+x demo.sh
https://crontab.guru/

Related

cron job not running as root user in digital ocean droplet

I created this test bash script
#! /bin/bash
echo "hello"
mkdir "/karan/washere"
cron job i created, i want to run this cron job to run every min and want the log
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log
I am signed in the droplet as root user
I also have given the permission for the script, using
sudo chmod u+x test.sh
I tried to log the syslog using
sudo grep CRON /var/log/syslog
but didn't show there also,
let me know if you need any more info or other context,
cron uses sh to interpret the commands, not bash. sh has less features and the &> is not valid in sh.
A better way for your line in the crontab would be:
1 * * * * /user/local/bin/bash /root/test.sh >> /root/crontest.log 2>> /root/crontest.err
This will append to the logging instead of overwriting, and it will separate log from errors.
If you want, however, you can force cron to use bash instead of sh. Your crontab should then be:
SHELL=/bin/bash
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log

FreeBSd rc.d source another file

On startup I manually run source /usr/local/etc/credentials.sh.
I've made a script /usr/local/etc/rc.d/load_credentials to run command for me at start-up. However the script does not succeed:
$ /usr/local/etc/rc.d/load_credentials start
eval: source: not found
load_credentials
The script is based on the example in rc-scripting docs.
#!/bin/sh
. /etc/rc.subr
name="load_credentials"
start_cmd="${name}_start"
stop_cmd=":"
load_credentials_start()
{
source /usr/local/etc/psql_creds.sh
}
load_rc_config $name
run_rc_command "$1"
credentials.sh
export DB_USER="JimmyJohn"
export DB_PASS="password123"
EDITs
Moved files as advised by #Michael-O
Based on your comment
will the credentials be available for crontasks running from my user?
it would seem you wish this to work in crontasks.
Here is a sample cronjob that I tested working and it does indeed work in crontasks.
Make a file in /usr/local/bin/crondo.sh
# chmod +x /usr/local/bin/crondo.sh
# cat /usr/local/bin/crondo.sh
#!/bin/sh
. /usr/local/etc/psql_creds.sh
echo $DB_USER
Add this line to crontab with crontab -e
# Run 22 minutes after every hour.
22 * * * * /usr/local/bin/crondo.sh > /tmp/out

Cron and Bash script: impossible to execute

I've made a bash script that execute a docker command to dump a MySQL database:
dump_db.sh
#!/bin/bash
time=$( date +%Y%m%d%H%M )
currdir=$( pwd )
cat $currdir/container_list | while read container; do
echo "" | docker exec -i $container mysqldump -u <user> -p<password> <dbname> > $currdir/$container-$time.sql
done
If i try to run the script manually, all works fine, but if i use cron, the script does not execute.
My crontab is:
PATH=<same environment>
26 17 * * * /bin/sh /path/to/script/dump_db.sh
as you can see, i've tried also to export PATH (and checked it via env command in the crontab), but nothing.
I've also tried with these lines
26 17 * * * /bin/bash /path/to/script/dump_db.sh
26 17 * * * /path/to/script/dump_db.sh
Furthermore, also a simple bash script, like this:
#!/bin/bash
touch test_touch.txt
does not work, while the simple touch command via cron it is ok.
Where am i wrong?
Kindly check if the script has execution permission for other user.
When you're running manually you're running with the user you're logged in.
But when its running from Cron its another user. Sot that user has to have the permission to execute that script.
You may check this post for more help

How to configure crontab to run ec2-automate-backup.sh script

Hi guys am trying to automate the backup of snapshots for my ec2 volumes on Amazon. I am following the ec2-automate-backup script by Collin Johnson
If run the command on command line it is creating the snapshot (working):
ubuntu#linuxserver:/usr/local/ec2/scripts$ sudo ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
For testing purposes if i create a crontab its not working
0 10 * * * ubuntu /usr/local/ec2/scripts/ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c /usr/local/ec2/scripts/cron-primer.sh -r "eu-west-1"
Where is my problem here am running the script on ubuntu 14.04 - Amazon?
In crontab file, to execute a shell script you can use one of the following approach:
1. Call the shell script direcly, i.e.
0 10 * * * /path/to/script.sh
where the script.sh should be made executable.
2. Execute the script by sh utility, i.e.
0 10 * * * sh /path/to/script.sh
here the script.sh need not be made executable.
Now, if in your case, you need to go to a specific path and then execute script, then :
Either provide the full path of the script in crontab file directly, or
Enclose the execution commands in other shell file, and execute the enclosing file from cron.
There are two possibilites:
You need root access to run the script. You can solve this by modifying root's crontab:
sudo crontab -e
See How to run a cron job using the sudo command
You need to be in the same directory as the script to execute it
0 10 * * * ubuntu cd /usr/local/ec2/scripts && ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
See What is the 'working directory' when cron executes a job

Crontab Unix Shell Script EOF

sqlplus -s u#t/t << EOF
drop table connection_backup;
CREATE TABLE connection_backup AS (SELECT * FROM connection);
drop table htbill_backup;
CREATE TABLE htbill_backup AS (SELECT * FROM htbill);
EXIT
EOF
Crontab -e File
*/1 * * * * sh ~/sql.sh | write $LOGNAME
due to the eof in first line crontab stops over their and does not execute further shell script
1. Get your shebang right
Find where your favoured shell is, using this command:
whereis bash
or
whereis sh
Then, insert a new line at the top of your sql.sh script that says
#!<FULL PATH TO SHELL>
An example of the above would be:
#!/bin/bash
2. Get your PATH right
Find where sqlplus is installed on your system, with
whereis sqlplus
Add a new second line in your script that says
export PATH=${PATH}:<SQLPLUS DIRECTORY ONLY>
An example would be
export PATH=${PATH}:/usr/local/bin # Note the word `sqlplus` is omitted - we only specify the directory where it lives
3. Make your script executable
Make your sql.sh script executable with
chmod +x sql.sh
4. Test your script outside cron
Test your script with
./sql.sh
and make sure it works prior to continuing.
5. Get the path to your script correct in cron
Find your login directory with
cd
pwd
Change your crontab script so it says:
*/1 * * * * <LOGIN DIRECTORY>/sql.sh

Resources