crontab suggested error :/bin/sh .backup.sh no such file or directory - linux

I use the command crontab -e to execute a shell script at regular intervals.
But something wrong with it.
The shell script is located in /opt/oneinstack/oneinstack,so my crontab is :
0 1 * * * cd /opt/oneinstack/oneinstack;./backup.sh >> /data/back.log
2>&1 &
The permission for the backup.sh file is 755.
The user using the crontab -e command is root
Message in /data/back.log is /bin/sh no such file or directory .Just like the title.
Manual execution the backup.sh is no problem.
What's wrong with it? And how can I fixed it?
Expect someone to help me.Thanks a lot!!

Try setting the following before the crontab list. For example, if this line is at the top:
0 1 * * * cd /opt/oneinstack/oneinstack;./backup.sh >> /data/back.log 2>&1 &
then it looks like this:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 1 * * * cd /opt/oneinstack/oneinstack;./backup.sh >> /data/back.log 2>&1 &
In addition:
Like #Ugur mentioned, it is unnecessary to cd, if you take care of the paths correctly in the backup.sh . For example:
ROOTPATH="/path/to/data"
TARTGETPATH="/path/to/target"
FileToBackup="${ROOTPATH}/myfile"
rsync $FileToBackup $TARGETPATH/

Related

Concatenate file output text with Crontab

I have followed up this question successfully, Using CRON jobs to visit url?, to maintain the following Cron task:
*/30 * * * * wget -O - https://example.com/operation/lazy-actions?lt=SOME_ACCESS_TOKEN_HERE >/dev/null 2>&1
The above Cron task works fine and it visits the URL periodically every 30 minutes.
However, the access token is recorded in a text file found in /home/myaccount/www/site/aToken.txt, the aToken file is very simple text file of one line which just contains the token string.
I have tried to read its contents and pass, using cat, it to the crontab command like the following:
*/30 * * * * wget -O - https://example.com/operation/lazy-actions?lt=|cat /home/myaccount/www/site/aToken.txt| >/dev/null 2>&1
However, the above solution has been failed to run the cronjob.
I edit Cronjobs using crontab -e using nano on Ubuntu 16.04
This is a quick solution that will do exactly what you want without the complicated one-liner:
Create this file in your myaccount -- You may also put it into your bin directory if you so desire just remember where you put it so you can call it from your CRON. Also make sure the user has permissions to read/write to the directory the sh file is in
wget.sh
#!/bin/bash
#simple cd -- change directory
cd /home/myaccount/www/site/
#grab token into variable aToken
aToken=`cat aToken.txt`
#simple cd -- move to wget directory
cd /wherever/you/want/the/wget/results/saved
#Notice the $ -- This is how we let the shell know that aToken is a variable = $aToken
#wget -O - https://example.com/operation/lazy-actions?lt=$aToken
wget -q -nv -O /tmp/wget.txt https://example.com/operation/lazy-actions?lt=$aToken >/dev/null 2>/dev/null
# You can writle logs etc etc afterward here. IE
echo "Job was successful" >> /dir/to/logs/success.log
Then simply call this file with your CRON like you are doing already.
*/30 * * * * sh /home/myaccount/www/site/wget.sh >/dev/null 2>&1
Built on that question, Concatenate in bash the output of two commands without newline character, I have got the following simple solution:
wget -O - https://example.com/operation/lazy-actions?lt="$(cat /home/myaccount/www/site/aToken.txt)" >/dev/null 2>&1
Where it able to read the contents of the text file and then echoed to the command stream.

Crontab doesn't execute R script

I'm trying to setup a Cron job on my system by adding the following line
17 12 * * * Rscript ~/path/to/file/script.R > ~/output_`date +\%d\%m\%y`.txt 2>&1
yet, I cannot see the file the output is being written to. I've consulted the following answers, but to no avail:
Why did my crontab not trigger
CronJob not running
When I run the following command on the terminal:
Rscript ~/path/to/file/script.R > ~/output_`date +\%d\%m\%y`.txt 2>&1
I get the output file as expected. I also added the following line to crontab:
* * * * * echo hi > ~/output.txt 2>&1
and it works just fine. I'm not sure what is wrong with the first command. Any help would be appreciated. Thanks.
Try Below trick, Create one script script.sh like below -
cat script.sh
Rscript ~/path/to/file/script.R > ~/output_$(date +\%d\%m\%y).txt 2>&1
And then create below entry in crontab.
17 12 * * * /bin/bash /path/to/script.sh
I fixed this by replacing Rscript in my crontab with /usr/local/bin/Rscript (or wherever your Rscript is located - do which Rscript to find out).

Run a script with arguments using crontab

I know this may have been answered earlier in various posts, but I've not been able to make this run myself.
I have a bash script (service.sh) that I would like to run every minute. It needs an argument to be passed (start in this case).
Using another script (test.sh) I am scheduling the cron expression for the above script:
echo "* * * * * /opt/service.sh start" > /opt/cronForSecops
crontab /opt/cronForSecops
I can see by using crontab -l that this is being set correctly as:
* * * * * /opt/service.sh start
However, the service.sh does not run, and I see no logs/files being created (which the service.sh file is supposed to do, when I run it normally).
Can anybody please guide me on where I am going wrong?
I was having this same issue where I using the following crontab:
0 23 * * * sudo -u myname /home/myname/bin/buildme.sh -f >> /home/myname/log.txt
And inside the bash script I was using this to get the -f option:
while getopts ":f" opt; do
case $opt in
f)
force_full=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
So I noticed that the option wasn't being honored when I ran this through cron for some reason. Well, adding /bin/bash to the cronjob fixed it right up. The new crontab is:
0 23 * * * sudo -u myname /bin/bash /home/myname/bin/buildme.sh -f >> /home/myname/log.txt
Hope it helps!
Try creating a simple wrapper script called /opt/start-service.sh with this content:
#!/bin/sh
/opt/service.sh start
and make sure it's executable then use
* * * * * /opt/start-service.sh
as the crontab entry

Cron job is not executing perl script

This is my perl script, it's just a test.
#!/usr/local/bin/perl
open (MYFILE, '>>data.txt');
print MYFILE "Worked!\n";
close (MYFILE);
I saved it as test.pl in cgi-bin/
When I run a shell command,
root#srv ./test.pl
it works fine and it adds "Worked!" in the data.txt file successfully.
But when I add this in cronjob to run every minute it doesn't work
code:
*/1 * * * * root cd /home/testing/public_html/cgi-bin;./test.pl
I've also tried
*/1 * * * * cd /home/testing/public_html/cgi-bin;./test.pl
I'm thinking it's my environment for cron that's not setup correctly, but I don't know how to fix it.
I've used this in crontab to execute the env
*/1 * * * * env > /home/tmp2/env.cron
And Here is the result:
SHELL=/bin/sh
USER=root
PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
PWD=/root
SHLVL=1
HOME=/root
LOGNAME=root
_=/bin/env
And then I exported the env of the shell i'm using to another file env.shell
results:
HOSTNAME=srv.testing.com
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=xxx.xx.xxx.xx 58048 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=0$
MAIL=/var/spool/mail/root
PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
PWD=/home/tmp2
JAVA_HOME=/usr/local/jdk
EDITOR=pico
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LS_OPTIONS=--color=tty -F -a -b -T 0
LOGNAME=root
VISUAL=pico
QTLIB=/usr/lib64/qt-3.3/lib
CVS_RSH=ssh
CLASSPATH=.:/usr/local/jdk/lib/classes.zip
SSH_CONNECTION=xxx.xx.xxx.xx 58048 xx.xx.xx.xx 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/bin/env
OLDPWD=/home/testing/public_html/cgi-bin
I copied the shell path
PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
And added it to /etc/crontab and I restart cron but it still didn't work. So, I changed it back to normal, and then I added it to crontab using crontab -e and it still didn't help.
I'm not sure what is going on,
Thank you for your time
I finally solved it!
I changed the path of crontab -e
PATH=/usr/local/jdk/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/root/bin
That's the same path in my shell you can get it if you do this
root # env > env.shell
root # nano env.shell
then copy the path from there, and paste it in crontab using crontab -e
Then, the last step was to add it to crontab -e, but after I added the path I had the cronjob in /etc/cron.d/sysstat so that's why it wasn't working.
Now it works and the main problem was the PATH.
Thank you for your time everyone.
Try
* * * * * root cd "/home/testing/public_html/cgi-bin"; ./test.pl

Crontab without crontab -e

I would like to add a crontab schedule by doing this in my server:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > crontab -e
Is there a way to do this without going doing crontab -e and then typing in the command?
Could try
1)nano /etc/crontab (or any other editor, e.g. emacs)
2)echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /etc/crontab
3)or put the contents of this into a file, then do "file > /etc/crontab"
like root:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /var/spool/cron/crontabs/username
We have the following setup in production on RHEL:
- a custom software starting sh in init.d , which
- handles cron start , stop , reload
- writes cron tasks into separate tmp file and loads this file with crontab -e
I have been only maintaining it for several months but it works like a charm ...
The proper fix is probably to use a file as specified in https://stackoverflow.com/a/4421284/377927, but it is possible to use tee to append a line to the crontab by doing e.g.:
echo "* * * * * ls" | EDITOR="tee -a" crontab -e
tee -a will append stdin to the file it gets specfied, the EDITOR variable tells crontab to use tee -a as editor.
If you have the whole crontab in a text file, you can upload a whole crontab to replace the old crontab by doing:
cat <crontab_text_file> | crontab -
This will wipe out your old crontab. Using '-' allows you to use standard input into the crontab.

Resources