How to get CRON to call in the correct PATHs
I used /etc/crontab
. I used vi
and entered in the PATHs I needed into this file and ran it as root. The normal crontab overwrites PATHs that you have set up. A good tutorial on how to do this.
The systemwide cron file looks like this:
This has the username field, as used by /etc/crontab.
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
42 6 * * * root run-parts --report /etc/cron.daily
47 6 * * 7 root run-parts --report /etc/cron.weekly
52 6 1 * * root run-parts --report /etc/cron.monthly
01 01 * * 1-5 root python /path/to/file.py
Using Relative Paths in Crontab
After many trials and research, I discovered that the solution was using the HOME=
variable, not the PATH=
variable, like so:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/var/www/html/crons
And then each of the lines would just look like:
*/2 * * * * root /usr/bin/php cronfile.php >> logs/cronfile_`date +\%Y\%m\%d`.log
Hope this helps someone else with the same issue I had in the future.
Bash: Prune find results using absolute paths
Note that the pattern match test applies to the whole file name,
starting from one of the start points named on the command line. It
would only make
sense to use an absolute path name here if the relevant start point is also an absolute path. This means that this command
will never match anything:find bar -path /foo/bar/myfile -print
You need to use the absolute path as search base too, so exchange the first .
(the starting point of the search) with the same absolute path you use for the -path
arguments.
find /usr -path "/usr/src/linux*" -prune -o -path "/usr/inclu*" -prune -o -name "*.txt" -print
This will list all *.txt
files but the content of any directory starting with /usr/src/linux*
or /usr/inclu*
.
Unable to run python script with shebang from /usr/local/bin
From the error:
bash: ./usr/local/bin/check_bios.py: No such file or directory
the cron job was looking for the check_bios.py script in the ./usr/local/bin
path.
From that, I guessed that the cron job was:
? ? ? ? ? ./usr/local/bin/check_bios.py
where ? represents the time values (deliberately left them as ? since they weren't mentioned in the post, nor are they relevant to the issue).
I believe it should be:
? ? ? ? ? . /usr/local/bin/check_bios.py
Cron, execute bash script as root, but one part (Python script) as user
Well, if the dummy folders did get created, that means the sudo statements work, so i'd say theres a 99%+ chance that python was infact started.
I'm guessing the problem is that you havent specified the path for the python file, and your working directory likely isn't what you're expecting it to be.
change:
sudo -u user_name /usr/bin/python3 python_script.py --arg01 --arg02
to something like
sudo -u user_name /usr/bin/python3 /path/to/your/python_script.py --arg01 --arg02
then test.
If that didn't solve the problem , then enable some logging, change the line to:
sudo -u user_name /usr/bin/python3 /path/to/your/python_script.py --arg01 --arg02 \
1> /home/user_name/projects/dummy_folder_00/log.txt 2>&1 ;
and test again, it should log STDOUT and STDERR to that file then.
Related Topics
How to Keep Environment Variables When Using Sudo
Get Destination Address of a Received Udp Packet
How to Set Environment Variable For Everyone Under My Linux System
Bluetooth Low Energy: Listening For Notifications/Indications in Linux
What's the Difference Between "Statically Linked" and "Not a Dynamic Executable" from Linux Ldd
How to Force a Makefile to Rebuild a Target
How to Specify the Library Version to Use At Link Time
Take Nth Column in a Text File
Is There a Way For Non-Root Processes to Bind to "Privileged" Ports on Linux
Get Exit Code of a Background Process
What Does "&" At the End of a Linux Command Mean
Bash While Read Loop Extremely Slow Compared to Cat, Why
How to Run a Shell Script When a File or Directory Changes
Automating Running Command on Linux from Windows Using Putty