Ruby and Crontab - Script is not producing results - ruby

I've looked for answers to this already without avail, so I turn here.
I'm running Ubuntu 12.04LTS, using RVM, running ruby 2.0.0p353.
I've written a script that, in a nutshell, modifies a file. Let's call this file access.txt.
Whenever I run the script by hand, it works beautifully, and modifies the file.
However, when I run this via crontab, it does not update the file. This is currently what I have in crontab.
0 * * * * /path/to/script.rb
I should also note that despite it being bad practice, I am running this as root.
I've also tried specifying it to run within a bash file.
0 * * * * /path/to/bash.sh
This also produces no results.
The cron log shows these jobs as running, and both have executable permissions.
Does anyone have any ideas?

Related

-bash: */10: No such file or directory when running cron job

In my current directory, I have a bash script called run_job.sh. This script runs perfectly fine.
I'm trying to schedule this script to run every 10 minutes using a cronjob. Here is the code that I am using:
*/10 * * * * run_job.sh
Now, when I do this, I get the following error:
-bash: */10: No such file or directory when running
I'm new to cron jobs so I'm not sure why I'm getting this error. Any help would be much appreciated.
The error message in your question suggests that you passed the crontab snippet to Bash somehow. That's not how you schedule a job; you type the command crontab -e at the Bash prompt and edit your Cron schedule in your favorite editor. That's where you would type in this snippet. When you save the file and exit the editor, cron will take your new schedule into use.
However, you should be aware that your normal PATH and other features of your interactive environment will not be available to cron jobs. At a minimum, you will probably need to specify the path to your script. If it's in $HOME/bin/run_job.sh, that's what you need to put in the final field in the crontab entry. (There may be more tweaks you have to do which can't be inferred from the information you have provided; see e.g. CronJob not running for further tips.)
What you are showing is the scheduling line from crontab (see man crontab). Further, I recommend against the /10 format, as it is not accepted by cron on all operating systems. To be safe, it is better to be explicit, as in:
0,10,20,30,40,50 * * * * run_job.sh

Python3 from shell script from cron ubuntu 16.04

I have a Python3 program that I need to execute 2 minutes past every hour.
Unable to get python to execute directly from cron, I’ve written a shell script with the commands in it.
The crontab to execute this shell script is this:
*/2 * * * * bash /home/john/PYTHONS/MASTER.sh
The shell script is simple. I’ve tried various versions, including this:
#!/bin/bash
cd /home/john/PYTHONS/
python3 ~/PYTHONS/COMPLEXPYTHON.py
python3 ~/PYTHONS/SIMPLEPYTHON.py
and including this:
#!/bin/bash
cd /home/john/PYTHONS/GDAXDEV/VPUMP
python3 $HOME/PYTHONS/COMPLEXPYTHON.py
python3 $HOME/PYTHONS/SIMPLEPYTHON.py
Here’s the issue:
If I execute the shell script from the command line BOTH python scripts run just fine.
If I execute the shell script from cron, ONLY THE SECOND python script runs. The first one just doesn’t run at all. And, I can’t seem to find out why, or how to fix it.
Any suggestions or help would be greatly appreciated.
I see questions about this issue all around, and no single one of them solved the issue for me. Several hours of testing every combination of variables lead me to a solution that’s reliable.
I’m no longer using a shell script, but invoking python directly in cron.
First, we did this:
$ which python3
/home/john/[path to python]/bin/python3
So, we added the shebang in every one of the python files in the project:
#!/home/john/[path to python]/bin/python3
And, in cron we used the full path to that python3, as well as the full path to the WORKING.py file we wanted to run. We want the thing to run 5 minutes past every hour so:
5 * * * * /home/john/[path to python]/bin/python3 /home/john/[path to dir]/WORKING.py
However, the log files this produced were being placed in /home/john/, and I wanted them in the specific directory. Also, there are a load of additional python files in there – with the same names as python files in nearby directories – so I wanted to be certain that any call gets the right one.
So, I added a CD command at the beginning of the cron command:
5 * * * * cd /home/john/[path to dir]/ && /home/john/[path to python]/bin/python3 /home/john/path to dir]/WORKING.py
It’s been running reliably ever since.
I’m grateful for the resources at stackoverflow and the great and fast help I’ve received in this community, and I hope this entry helps save someone else a whole afternoon of frustration.

How do I redirect the output and errors from a cronjob to separate files?

I have a cronjob set every 3 minutes to run a bash script. Now, I wish to separate the output and the errors to two separate files. I did read the answer given in this AskUbuntu question and tried */3 * * * * ./script.sh>>output.log 2>>errors.log (>>, since I'm creating the file in case it doesn't exist), but this seems to redirect the output again to errors.log. What am I doing wrong?
That should work just fine.
Perhaps your problem is that you are not using absolute paths for the script and the log files?

Cron Bad Minute Error: "home.cron":0: bad minute crontab: errors in crontab file, can't install

I am creating the cron on Mac OS Terminal, here is the code:
contents of home.cron:
* * * * * /users/username/desktop/forTrump/script.sh
then I run it like this:
crontab home.cron
then I get this error:
"home.cron":0: bad minute crontab: errors in crontab file, can't install
Here is the shell file that is opened above, I tested and this runs perfectly by executing ./script.sh from os terminal but I can't seem to run it in cron w/o getting that error.
#!/bin/sh
python /Users/username/Desktop/forTrump/test.py
I already ensure that there were no line breaks causing the bad minute error, thanks in advance for your help.
I have encountered with similar issue today:
"/tmp/crontab.vjQAiZ" 1L, 14C written
crontab: installing new crontab
"/tmp/crontab.vjQAiZ":0: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit?
Same error was raised even I tried to add ABSOLUTELY correct entry in my crontab list.
As usual I have leart google advices. But notihng has helped.
The solution in my case: 100% usage of /var/log/ directory.
It looks like cron can't update own logs and therefore writes similar error when you would like to save your changes.
When I deleted extra log files, issue was solved.
Good luck!
This error (especially if the error indicates that the invalid minute is at column 0) will also occur if the volume where cron files are stored is out of space.
According to crontab.guru both * * * * * and *\1 * * * * are essentially the same thing although you may want to try the alternative just in case your system does not like one or the other.
However in your posted home.cron the path is wrong.
This:
* * * * * /users/username/desktop/forTrump/script.sh
should be:
* * * * * /Users/username/Desktop/forTrump/script.sh
However if the script requires elevated privileges then you will need to use the root crontab and the line used is slightly different due to the addition of a user column before the command like this:
* * * * * root /Users/username/Desktop/forTrump/script.sh
To install that script as root first switch to root like this:
sudo su
followed by:
crontab home.cron
Also seeing you are on Mac OS X make sure the line in home.cron is terminated with just \n and not \r or \n\r
See also: https://www.dougv.com/2006/12/fixing-a-bad-minute-error-message-when-trying-to-use-crontab-with-certain-unix-text-editors/
This problem most often occurs because you’re using a text editor, such as pico, that fakes word wrapping by adding a newline when it reaches a certain column position.
Crontab delimits jobs with line breaks (newlines). Each job occupies one line. Therefore, if crontab sees anything other than an integer in the first column of a line, it throws the “bad minute” error, since the minute argument is the first one crontab encounters.
The simple fix is to go to delete the line breaks added by pico / your *nix editor. You can most easily do that by putting your cursor on the first character of each extra line, then hit the backspace key until that line is joined back up with the previous one, and repeating the process until your entire cron command is on one line.
In my case I got this error inside a docker container. The reason for the error is that there was an empty line at the bottom of the file, removing which the issue went away.
When exactly shall your cron job run - every minute?
m h dom mon dow command
*/1 * * * * /users/username/desktop/againstTrump/script.sh

Crontab won't run sed shell

I have a cron job to run a sed shell called sedcmd.sh to pre-process some json data. When i am in the proper directory I manually run it with
. ./sedcmd.sh
And it works. The shell itself works fine.
for reference one of the commands inside looks like
sed -i '/^\s*$/d' /home/school/Desktop/Programs/rawjsondata.txt
my cronjob looks like
5 * * * * . ./home/school/Desktop/Programs/sedcmd.sh
I get the error "No such file or directory found". What am I doing wrong. I've triple checked for any random spelling errors. I also can't seem to run sedcmd.sh from any other directory even when i give the entire file path, so its definitely something I'm doing wrong. My thoughts for solutions are either
I should add sedcmd.sh to my $PATH or bashrc so i can call it from anywhere. Which I don't know how to do.
OR
figure out how to call it correctly from crontab. Which I also dont know how to do.
When you are running a script from terminal, you do:
./script_name.sh but to execute the same script from crontab you do something like 5 * * * * /path/to/script/script_name.sh
As Sam has got this answer from the comments,posting the answer as community wiki.

Resources