Raspberry crontab script running - cron

I'm trying to run a command by cron in Raspbian.
If I run ./sec_cam.sh, than my script runs, If I try to run it via crontab every 5 min, than nothing happens.
crontab -e shows me the followings:
*/5 * * * * ./sec_cam.sh
Did I configure the crontab wrong?
Thx in advance

scripts started from a cronjob are not setup with your usual environment, especially not with your current working directory (referenced by the . in ./sec_cam.sh). so to make this work you should specify a full path name like /home/user/sec_cam.sh

Related

Simple cron job for generating random wallpaper with nitrogen

I am trying to create a cron job that will change my desktop wallpaper every minute. I created a new job using crontab -e.
*/1 * * * * /usr/bin/nitrogen --set-zoom-fill --random /home/rkr/Pictures/wallpapers-master/
The command works on the shell very well. The cron job is also running and visible in /var/log/syslog.
I tried to debug it by sending the output to a log file, which reports a warning about nitrogen :
(nitrogen:10468): Gtk-WARNING **: 09:37:01.654: cannot open display:
I don't think that is the issue though. To further verify if my cron is working I tried :
*/1 * * * * touch /home/rkr/Videos/file and the file was created.
Now I am confused if this is the issue with nitrogen.
Hello
It depends...
I think the issue is the environment.
Having a Terminal open in X and type:
env | grep DISPLAY
This gives the information you need to set the Display for nitrogen.
Because the cronjob sh/bash dont have this variable set.
Now your best friend is env too for starting nitrogen in crontab.
env -i DISPLAY=:0.0 nitrogen
...much fun.

How to make crontab run new version of my script

I have currently running python script on debian system. Now because of some reasons I changed this script and updated cron, but nothing has changed. Also, I tried to save this cron in different file and create new cron - line with job appears, but script doesn't work.
CRON[22310] (root) CMD ( /usr/bin/python /home/radmin/test/test.py)
from /etc/crontab for new script:
*/1 * * * * root /usr/bin/python /home/radmin/test/test.py
for old script:
*/1 * * * * root python /home/radmin/base.py
Script runs correctly without cron.
Tried restarting and reloading cron.
It looks like cron dosen't recognize the root command you add.
try instead opening cron using sudo crontab -e and then add your code:
*/1 * * * * /usr/bin/python /home/radmin/test/test.py
by opening it up with sudo it will add it to the root user cron jobs.
Problem was in python code. Crontab is okay. I'm using python lib "requests" and there is a method to get post request content - ".text", so this method doesn't want to work in cron (empty error logs while running) (still don't know why). So changing .text to .content solved this issue.

Shell script cronjob

I need a shell script that would run two .sh files which are in the directory: /opt/tomcat-latest/bin
#!/bin/sh
cd /opt/tomcat-latest/bin
./shutdown.sh
./startup.sh
Would this code achieve my goal? If so how do I make it on a cron job that runs every 2 hours? I have a Linux Centos VPS and DirectAdmin admin panel.
I think the easiest solution would be to run directly the commands with its full path from cron, instead of using a sh script.
Something like this in the crontab would work :
* */2 * * * /opt/tomcat-latest/bin/shutdown.sh && /opt/tomcat-latest/bin/startup.sh
That would run every 2 hours
you can edit crontab with crontab -e and check the crontab syntax here : https://fr.wikipedia.org/wiki/Crontab

Cron job does not run in RHEL

I'm running RHEL and I'm trying to set up a cron job to run a shell script every 5 minutes.
Following the directions here: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/ch-Automating_System_Tasks.html#s2-configuring-cron-jobs
I have service crond start and chkconfig crond on. Then I edited /etc/crontab and added:
*/5 * * * * my-user /path/to/shell.sh
I did a chmod +x shell.sh. And I made sure to add a new line character at the end.
I'm expecting it to run every 5 minutes but it never executes.
What am I doing wrong?
Simply try to add the cronjob entry and check the script is working fine or not by taking the viewable output in the script.
echo "test time - $(date)" > script.sh
chmod +x script.sh
crontab -e
Then enter the cronjob as below,
*/5 * * * * sh /path/to/script.sh > /path/to/log.file
Check if the log is writing correctly. If its fine, better cross check the script that you are trying to execute via cron. Otherwise it will be a cron issue.

Downloading files with bash via cron

I have build a bash script that gets .tar.gz files from IMDb and writes to a log file, the script works when run on its own as I can see the folder with the files present, but when I run the script via cron it doesn't work. Would this be due to permissions? I have edited the sudo crontab file, but I'm not sure what else I need to do.
Try this solution:
Cronjob is a file that contains your job:
cat cronjob
* * * * * bash /path/to/script.sh >> /path/to/log.txt
Then you should set executable permission and start cron service:
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
After that you should tell cron service to run cronjob file:
crontab cronjob
Your script should download a file.
If your script doesn't run you should run it from good path[full path], so your cronjob file would be something like this:
* * * * * /bin/bash /path/to/script.sh >> /path/to/log.txt

Resources