How to autorun crons? - bash

I'm trying to get a file to be automatically run by crontab, contents like so:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
*/5 * * * * php /path/to/script
I've put this file called prod.cron into the /etc/cron.d folder but can't seem to get it to run on it's own. Running the script directly via command line shows that it works okay, but not sure if the files in cron.d get auto detected or do I need to do something else to init them?

Note that directory /etc/cron.d is intended to be used by packages, crontabs should be installed into an users crontab. That being said, check out the following Cron Issues:
The script is not having execute permissions. Use chmod a+x to provide permissions.
In the case of php, perl or other scripting language, the path to interpretor or program is not correct. Always provide actual path to
the interpretor while defining command to run in cron. To find out
where the program is located, use whereis command. For e.g.
for php, whereis php. for perl, whereis php, etc.
The environment variables required to run the script are unavailable. The cron doesn't use the same environment of a user. It
uses its own environment while running the commands. So the variables
defined in .login or .profile are not visible to him. The most common
issue is related to PATH variable which is not similar.

There are two flavors of cron file.
Files installed under /etc/cron.d are system cron files. They require the 6th field on each line to be the user account used to run the command. See other files under /etc/cron.d for examples.
Non-system cron files just have the usual 5 fields to specify when the job runs, followed by the command to run. They're installed by the crontab command, executed by the user account that will run the command(s). This is probably what you wnat to use.
Either add root as the 6th field of the relevant line, or run crontab prod.cron as root (or as whatever account should run the command).
And of course make sure the php command is in the specified $PATH.

Related

Cannot source .bashrc inside cronjob

I'm trying to run a python script on a cronjob. To access environment variables in that script, I've wrapped the python script in a short shell script. However, cron does not appear to be accessing my environment correctly, as I'm still getting errors in my python script about not being able to access environment variables. If you consider the below shell script
#!/bin/bash
source /home/jfeldman/.bashrc
env
Running this script from a cronjob yields only this output
SHELL=/bin/sh
PATH=/usr/bin:/bin
PWD=/home/jfeldman
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/jfeldman
LOGNAME=jfeldman
_=/usr/bin/env
Whereas running that shell script from the command line yields a much larger list of environment variables, including the API tokens that I want to access from my python script. Additionally, my PATH variable is considerably shorter than the cronjob version of env
Does anyone know why Cronjobs couldn't access my environment correctly even if the cronjob is running a script that explicitly declares source /home/jfeldman/.bashrc?
This is the line from the crontab fwiw
*/5 * * * * /home/jfeldman/crons/test_cron.sh >> /home/jfeldman/crons/test.log 2>&1$`
Edit: after some testing it seems like cron can source my virtual environment just fine, just not .bashrc, which seems incredibly bizarre.
Shell sh is not bash. sh doesn't know the command source and possibly wouldn't import .bashrc without errors when using it's source command . /home/user/.bashrc for sourcing it.
Put the line
SHELL=/bin/bash
on top of your crontab to run the jobs with bash.
Sourcing a users .bashrc to a cronjob running as root user is a bad idea, you could think about creating a job in /etc/cron.d/ instead, and specify a user other than root running the job.
5/* * * * * jfeldman /home/jfeldman/cron_runner.sh
.bashrc can change over time it makes sense to copy everyting into the cron_runner.sh or even better - create a new user with own home dir just for running the cronjob in a seperate home dir.

Raspbian: Reset Bash environment variables

I was trying to get a crontab working on my Raspberry PI and I think I messed up my environment variables. I can execute a file from the GUI by right-clicking and choosing execute. However I cannot get the same file to run from command line. I can use ls to see the file (ChromeTab.sh), but when I type ChromeTab.sh, I get "bash: ChromeTab.sh: command not found".
I think I messed up my environment variables when I put this in the crontab.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I followed the examples in Script doesn't run via crontab but works fine standalone.
Any idea what I'm doing wrong?
UPDATE:
OK,
Let me clarify what efforts I took on my part BEFORE posting my question on stackoverflow before getting anymore downvotes.
First of all thanks S. Adam Nissley for your suggestions.
In answer to your steps listed above.
Running this from home path, or fully qualified path does Not work as stated.
Error: bash: ChromeTab.sh: command not found
./ChromeTab.sh
I have also ensured read/write and execute permissions on the file with
chmod +x ./ChromeTab.sh
Also, my bash script starts off with the following shebang
#!/bin/sh
So, what i'm trying to say is, regardless of using crontab or not the issue at hand is that I can not even execute the script from command line. This started happening after I was messing around with the environment variables in the crontab. I'm looking for a way to revert to the situation where I can at least run/execute bash commands from the terminal.
The only way I can effectively execute this script is (right-click execute) through the GUI.
Assuming you are in the same directory as your script, you should just be able to enter
./ChromeTab.sh
If it does not execute, make sure it is executable with the command
chmod +x ./ChromeTab.sh
Or
chmod 755 ./ChromeTab.sh
And if it still won't execute, make sure it has an appropriate hashbang on the very first line of the script like #!/bin/sh or #!/bin/bash
When you add it to your crontab, make sure it has the full path like
/home/pi/bin/ChromeTab.sh <br/>
EDIT: Default PATH and SHELL for Raspbian
You can check your PATH and SHELL environmental variables from the command line as follows:
echo $SHELL
echo $PATH
The default PATH for Rasbian is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
And the default SHELL is:
/bin/bash
So if you need to set those it is as simple as:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
SHELL=/bin/bash
If you are having other issues with your environment, you may want to disable some of your local settings to see if the problem is in your profile. You can list all files with ls -a, which includes hidden files. Raspbian typically has a .bashrc and a .profile in each user's home directory. To disable them simple rename them:
mv .bashrc .bashrc_disabled
mv .profile .profile_disabled
If that solves the problem, you can inspect the files and make the necessary corrections before renaming them back to their original names.

Robot Framework script won't execute properly from cron

I'm trying to set up some automated tests using the Robot Framework's pybot script that allows you to execute Robot tests via shell. pybot, for those not familiar, is installed along with the Robot Framework to /usr/local/bin, and simply does:
#!/usr/bin/python
import sys
from robot import run_cli
run_cli(sys.argv[1:])
Effectively allowing you to evoke it "natively" from bash rather than python. Now, the way I'm trying to call pybot is as follows:
cd /var/www/parsingdev/meTypeset/tests && for file in ./*.txt; do pybot -d $(echo "/var/www/parsingdev/robot/"$(echo $file | sed -e "s/\.\///g" -e "s/\.txt//g")) $file; done
If I run this with sudo bash -c and wrap it all in single quotes, it works fine. If I save it to /opt/robottest.sh with a #!/bin/bash declaration as a first line and the command as the second line, and call it from the shell, it also works fine. However, none of the expected output files from the Robot tests are created when I use either of those methods to run it via root's cron, so something's not working.
I don't get it, though -- if I just do sudo env, it shows that /usr/local/bin is in the PATH, so it shouldn't be failing to find pybot, and otherwise I don't know why running it as sudo should be different from running it from root's crontab.
Thoughts appreciated!
Environment for root cron (or cron in general) is not identical to that when running a root bash. I suspect that pybot is not in the PATH used by cron.
In general, you want to make sure all paths are fully-qualified or PATH explicitly declared for cron tasks.

Script shell to automate deployment site

I'm trying to write a script shell (Mac OS X) for deploying my Awestruct site within a launch agent.
So, I have to generate site before deploying it. The command to generate site is :
awestruct -g
My script is myscript.sh :
cd /my/site/structure/base/directory
awestruct -g
This script is launched by the system when an event occurs.
But the problem is it doesn't know about awestruct...
My awestruct program is a part of my ruby installation and is added to my PATH variable.
When I run the script manually, it works (because it's my user who is lauynching it)
When the system detect the event and runs the script, it results as :
awestruct: command not found
The problem is the PATH...
If it works manually, then in the same prompt where it works, run command:
which awestruct
That will print the program file with full path, let's assume /usr/local/bin/awestruct, but use whatever it really is. Then use that in your script, for example:
cd /my/site/structure/base/directory
/usr/local/bin/awestruct -g
This assumes that there are no other environment variables, only defined for your account, which awestruct needs. If there are, then find out what they are, and add them to your script before running awestruct, for example with line:
export AWESTRUCT_ENVIRONMENT_VARIABLE=foobar
(Note: When you run the script normally like any program, that will not change the parent shell environment.)
You can also add the path to the executable in the PATH of the user that run the script and gets the error.
You could try something like :
$ su - <user_that_run_the_script>
$ echo "export PATH=$PATH:$(which awestruct)" >> ~/.bash_profile
$ source ~/.bash_profile
(For Linux users, use ~/.bashrc instead of ~/.bash_profile)

Shell script to use my login environment

I have crontab running a shell script periodically. I need the script to run in the same environment that I usually log in. Can I just simply add this line in 2nd line of the script (after shebang).
source /home/<my username>/.cshrc
Or what's the proper way to set the cron shell process to use my login environment?
PS: I am quite sure which exactly setting is needed by my script, so I can only source the whole .cshrc.
Try something like that:
sudo su - <user> -c <cmd>
Of course you have to alter the sudoers file first.
Take a look at the man page.
hth

Resources