Simple cron job for generating random wallpaper with nitrogen - cron

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.

Related

Setting up a cronjob on Google Compute Engine

I am new to setting up cronjobs and I'm trying to do it on a virtual machine in google compute engine. After a bit of research, I found this StackOverflow question: Running Python script at Regular intervals using Cron in Virtual Machine (Google Cloud Platform)
As per the answer, I managed to enter the crontab -e edit mode and set up a test cronjob like 10 8 * * * /usr/bin/python /scripts/kite-data-pull/dataPull.py. I also checked the system time, which was in UTC, and entered the time according to that.
The step I'm supposed to take, as per the answer, is to run sudo systemctl restart cron which is throwing an error for me:
sudo systemctl restart cron
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
Any suggestions on what I can do to set up this cronjob correctly?
Edit a cron jobs with crontab -e and inset a line:
* * * * * echo test123 > /your_homedir_path/file.log
That will write test123 every minute into file.log file.
Then do tail if and wait a couple minutes. You should see test123 lines appearing in the file (and screen).
If it runs try running your python file but first make your .py file executable with "chmod +x script.py"
Here you can find my reply to similar question.

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.

task in crontab does not work

I want to automatically start a program if it crashes (check if the process exists, if not, restart it), so using crontab is a simple solution, but
I creatd a simple crontab task using crontab -e or in /etc/crontab file like this:
* * * * * /usr/bin/gnome-terminal
it doesn't work, it never launches gnome-terminal, I can see it executes the task in /var/log/syslog (Ubuntu) or /var/log/cron (CentOS), the gnome-terminal never comes up.
if you say since gnome-terminal is a gui program, then, this script to create bluetooth service won't work either :
using crontab -e or /etc/crontab:
* * * * * /home/username/run-bt
run-bt:
#!/bin/sh
# find bt pid
/usr/bin/pgrep bluetoothd
# if not running
if [ $? -ne 0 ]
then
/usr/sbin/service bluetooth restart
fi⏎
I already made the run-bt script executable, but bluetooth service won't start even if I stop the bluetooth service manually.
but this:
* * * * * /bin/ls > /tmp/ls.output
works as expected.
What's wrong?
Actually crontab does not load all the environment variables of the current user it runs for, just a few of them.
Maybe some of those environment variables are required by the scripts you try to run?

Raspberry crontab script running

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

Using Cron to Reboot

I'm using a Raspberry Pi for a status display, but for whatever reason it gets incredabbly sluggish after a day or so of running so I wanted to reboot it every day so I setup a cron job to do that every morning at 8:50. But, it doesn't seem to be working. Is there anything special about using cron to do a reboot?
This is my crontab for the root user:
# m h dom mon dow command
50 8 * * * shutdown now -r >> /var/log/cron.log
0,30 * * * * date >> /var/log/cron.log
The second line works just fine, but I can't seem to get the restart command to work. It doesn't even output anything to the log.
Try using the fully specified path to shutdown. date may be in the PATH in roots cron environment, /sbin may not be looked up.
You need to edit the root user crontab
sudo crontab -e
then..
50 8 * * * reboot
Save and exit.

Resources