Currently trying to set up a cron job
with a python
script that I have git cloned from here. The hierarchy to reach my script can be described as below:
/home
|
|
/Daily-Reddit-Wallpaper
|
|
change_wallpaper_reddit.py
Now this works when I use the command, python change_wallpaper_reddit.py --time new
inside the Daily_Reddit_Wallpapers
folder. However, when I try the command, * * * * * python ./change_wallpaper_reddit.py --time new
, I get the error:
change_wallpaper_reddit.py: command not found
When I try to invoke * * * * * python ~/Daily-Reddit-Wallpaper/change_wallpaper_reddit.py
, I get:
usage: anaconda [-h] [--show-traceback] [--hide-traceback] [-v] [-q] [--color]
[--no-color] [-V] [-t TOKEN] [-s SITE]
...
anaconda: error: argument : invalid choice: 'Daily-Reddit-Wallpaper' (choose from 'auth', u'label', u'channel', 'config', u'copy', u'download', 'groups', u'login', 'logout', u'notebook', 'package', 'remove', 'search', 'show', u'upload', u'whoami')
I do not understand why this happens.
Please be aware the a cronjab executes in a shell that has a limited environment setup. By that I mean that when you open a terminal and enter env you will see a lot of environment variables; one of the most important ones is PATH. The cron job does not login so to speak, thus .profile files are not executed. So in your script you must make sure to set or complement environment variables like PATH.
Also, a cron entry should not use the ~ but put the full path.
In my system I created a small script to list the environment variables that are set when the script is started in cron. As you see a lot less than when in a terminal:
Proper scripts start with a shebang expression, some text explaining what the script will do (you might forget after a few months) and then setting environment variables. A small example (NB willem is my user name:
To put the script in cron, enter
crontab -e
:You are in vi so go to end of file and add:
* * * * * /home/willem/prog/Hello
Close and save, and view your crontab entry/entries: crontab -l
The problem is that, the script isn't designed to work with Cron. It uses few environment variables, which are not accessible from Cron and they are different, depending on the current user's desktop environment. This is the reason on its page to be described another way for running on startup. But it is possible to set values of these variables while the CronJob is running.
For example, when it is the default Ubuntu's desktop environment, the search key words should become: 'gsettings' and 'cron', then our search will lead us to wired topics as: Background not changing using gsettings from cron, where we could find additional explanations as:
Run: Daily-Reddit-Wallpaper through Cron via Startup script
Here we will create a startup script, which shall set the necessary environment variables depending of the chosen (by an argument) desktop environment.
1. First cloned Daily-Reddit-Wallpaper and also install the dependencies:
2. Create the script file - change_wallpaper_reddit.sh:
The content of the script is:
This script has one argument
$1
, that determine its behaviour depending of the chosen (from you) desktop environment (DE). The possible values are:gnome
orunity
orempty
(default) - when you use the default Ubuntu DE;kde
- when you use KUbuntu DE;lxde
- when you use LUbuntu DE;mate
- when you use Ubuntu MATE DE;xfce4
- when you use XUbuntu DE.Also you can customise these initial parameters:
SHOME=
set the folder where Daily-Reddit-Wallpaper is located into your system.DIR=
set the output folder in the home directory to save the Wallpapers to - the default value (Pictures/Wallpapers
) is used in the above script.TIME=
set the value of the--time
parameter ofchange_wallpaper_reddit.py
.3. Create CronJob (
crontab -e
), that executeschange_wallpaper_reddit.sh
(at every hour for example):If you use the default Ubuntu DE, this CronJob could be:
also this syntax will bring same result:
If you use KUbuntu DE, for example, this CronJob could be:
For troubleshooting check the log file:
cat /home/$USER/Daily-Reddit-Wallpaper/cron.log
Voilà. It's working!
References and further reding: