Crontab run a ruby file not work - ruby

*/1 * * * * ~/.rbenv/versions/2.1.5/bin/ruby /Users/zhangjian/Desktop/mail.rb
*/1 * * * * /usr/bin/ruby /Users/zhangjian/Desktop/OHS_Project/ohs_server/rest/notification.rb
I write these 2 under crontab e but it doesn't work. I try run the file directively, everything works.
aFile = File.new("./time.txt", "a+")
if aFile
aFile.syswrite(Time.new.inspect)
aFile.syswrite(" ")
else
puts "Unable to open file!"
end
how can I fix it?

Try using the full path without ~ in the crontab. You should also try to use an absolute path when referencing time.txt.

Related

Execute several instances of script same time

I have a crontable that executes a script with different args
0,5,10 * * * * /path/to/logScript "script2"
0,5,10 * * * * /path/to/logScript "script1"
0,5,10 * * * * /path/to/logScript "script3"
script
#!/bin/bash
script=$1
$script args >/path/tmp/log.out 2>/path/tmp/log.err
if [ ! -s /path/tmp/log.err ]; then
echo -n "$(date) success: " >> /path/logfile
cat /path/tmp/log.out >> /path/logfile
else
echo -n "$(date) errors: " >> /path/logfile
cat /path/tmp/log.err >> /path/logfile
fi
The issue that I think i'm having is with the execution of the script in the same time. The script doen't get the right value of return (to know wheter it's stderr or stdout). If i execute the crontable lines one after one in a terminal it works fine but when it's executed automatically the data i get is incorrect.
I tried to solve this by making this changement to crontable but i still have the same issue.
0,5,10 * * * * /path/to/logScript "script2"&
0,5,10 * * * * /path/to/logScript "script1"&
0,5,10 * * * * /path/to/logScript "script3"&
if all commands are running at same time, think you could combine the command in a unique one, using & at end of commands to push them to background mode, like this:
0,5,10 * * * * /bin/bash -c '/path/to/logScript "script2" & /path/to/logScript "script1" & /path/to/logScript "script3" & '
you need to run it as a background process (add "&" at the end of the process line so you can "spawn" them)
you can also wrap it into a loop with i being number of processes and run it within the same file.
func () {
#do this
#do that
#hit him with a bat
}
then in sh
#!/bin/bash
for i in $(seq 0 5);
do
function() &
echo "child pid: " $!
done

echoing mutiple variable + asterisk in bash

I am trying to echo / redirect to file two or three variables and asterisk;
However, as long as I am trying to include more than only one then my variables disappears.
Loading variables from file.
var:
CMD=/home/user1/command.sh
HOUR=17
MN=00
Here is a snippet of my script:
#!/bin/bash
. var
if [ "${MN}" == "00" ];
then
MN=0
fi
echo "${MN} ${HOUR} * * * user1 ${CMD}" >> ./test.log
the output look like the following which does not include all variables:
* * * root /home/user1/command.sh
It seems that for some reason while loading these variables from file; these cannot be reused.
As soon as I set them manually as part of the script itself, the output is as below and correct:
0 17 * * * root /home/user1/command.sh
Any help would be appreciated,
Many thanks,
AL.
Crontab execute that script not in /home/user1/ folder.
So you need use /home/user1/var for variable include or do
cd /home/user1/
before include file with variables.

Path not get set from crontab

Very simple and yet not working. Path not get set.
crontab:
* * * * * source /home/inst1/.profile; /home/inst1/Scripts/test.sh > /home/inst1/Scripts/test.log 2>&1
.profile:
[..whatever..]
PATH=/tmp
export PATH
test.sh:
#!/usr/bin/bash
echo $PATH
cat to test.log gives me:
/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java5/jre/bin:/usr/java5/bin
I guess it related to AIX environment, but I have no idea - seems simple but I'm missing something somewhere.
Try this:
* * * * * bash -c "source /home/inst1/.profile; /home/inst1/Scripts/test.sh" > /home/inst1/Scripts/test.log 2>&1
cron uses /bin/sh by default, while source is not a POSIX shell command. You need to use . instead:
* * * * * . /home/inst1/.profile; /home/inst1/Scripts/test.sh > /home/inst1/Scripts/test.log 2>&1

Crontab - Shell Script

I have the following command in my crontab:
* * * * * root /home/amith/m.sh >dev/null 2>&1
where m.sh consists of:
#!/bin/sh
curl -0 "http://www.google.com" > /home/amith/Desktop/h2
but the command in shell script is not executing at all.
Can anyone please tell me the solution?
First be sure that your script /home/amith/m.sh is running correctly!
Your crontab entry is wrong you dont need root before script. Also your redirection to /dev/null is not good you are missing / before dev
You can set env varibale for SHELL in crontab with this
crontab -e
SHELL=/bin/sh
Then add your script:
* * * * * /home/amith/m.sh >/dev/null 2>&1
* * * * * cd /full/path; sh m.sh;
First, change the directory into your file location (use cd). Then, bash it with sh command.
Change * * * * * with the time schedule. To run the file every minute * * * * * To run every hour (1:00, 2:00, 3:00 and so on) use 00 * * * * To run everyday at 6 AM use 00 06 * * * To run the file on 9th March at 6 AM use 00 06 09 03 * The structure is minute hour date month day of week If you want to schedule it every Monday at 06 AM use this 00 06 * * 1 See this link http://www.pantz.org/software/cron/croninfo.html

Insert entry into crontab unless it already exists (as one-liner if possible) [duplicate]

This question already has answers here:
How can I programmatically create a new cron job?
(20 answers)
Closed 4 years ago.
What's the preferred method to insert an entry into /etc/crontab unless it exists, preferably using a one-liner?
Here's my example entry I wish to place into /etc/crontab unless it already exists in there.
*/1 * * * * some_user python /mount/share/script.py
I'm on CentOS 6.6 and so far I have this:
if grep "*/1 * * * * some_user python /mount/share/script.py" /etc/crontab; then echo "Entry already in crontab"; else echo "*/1 * * * * some_user python /mount/share/script.py" >> /etc/crontab; fi
You can do this:
grep 'some_user python /mount/share/script.py' /etc/crontab || echo '*/1 * * * * some_user python /mount/share/script.py' >> /etc/crontab
If the line is absent, grep will return 1, so the right hand side of the or || will be executed.
Factoring out the filename & using the q & F options
file="/etc/crontab"; grep -qF "some_user python /mount/share/script.py" "$file" || echo "*/1 * * * * some_user python /mount/share/script.py"
You can do it like this:
if grep "\*\/5 \* \* \* \* /usr/local/bin/test.sh" /var/spool/cron/root; then echo "Entry already in crontab"; else echo "*/5 * * * * /usr/local/bin/test.sh" >> /var/spool/cron/root; fi
Or even more terse:
grep '\*\/12 \* \* \* \* /bin/yum makecache fast' /var/spool/cron/root \
|| echo '*/12 * * * * /bin/yum makecache fast' >> /var/spool/cron/root

Resources