how to use crontab to run a Graphical program such as "gedit" - cron

how to use crontab to run a Graphical program such as "gedit"
57 12 * * * gedit --display=localhost:0
Can not successfully open the program and display it.

--display=localhost:0 can be causing error ,
Following code will also do the trick
57 12 * * * export DISPLAY=:0 && /usr/bin/gedit
There is a reason why i added entire path of gedit ,
Utilities in /bin and /usr/bin can be opened using cron just by specifying its name so
/usr/bin/gedit or just gedit will work.
May a time command you use to start certain utility on terminal doesn't work using cron ,this is because cron passes a minimal set of environment variables to your jobs.May be required env PATH variable is not available with Cron hence it is not able to locate that utility.
you can find a detailed explanation in first reply here.

Related

crontab does not execute python script

I am facing an issue of crontab does not run the python script in Raspbian GNU/Linux 9.4 (stretch) installed on a Raspberry Pi3. I have done lots of research on this topic e.g. by following the troubleshooting guide in https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working/24527#24527 , but none of them solved my issue.
The python script that I want to run is located in
/home/pi/Documents/Fork_BookManager.py
I have made sure that anyone can execute, read and change content to the above file.
I can run this file through terminal by writing
usr/bin/python3 /home/pi/Documents/Fork_BookManager.py
I know the file is running, because the Fork_BookManager.py is using selenium to open a web browser, and I see this web browser being opened. There is no error observed when running the Fork_BookManager.py file from terminal.
In terminal executed
crontab -e
Using nano as editor, I first verified that crontab acutally worked by
* * * * * env > /home/pi/Documents/env.output
After a minute there is a env.output file in Documents folder with below parameters.
LANGUAGE=da_DK.UTF-8
HOME=/home/pi
LOGNAME=pi
PATH=/usr/bin:/bin
LANG=da_DK.UTF-8
SHELL=/bin/sh
LC_ALL=da_DK.UTF-8
PWD=/home/pi
I noticed that the path from crontab is different from the path when env is called from terminal as I will have the below path if I call env from terminal
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
So I know that crontab -e was working since I otherwise would not have got the env.output file created in Documents folder. I proceed by calling crontab -e from terminal and copied the path from env into the crontab like below
PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
* * * * * /usr/bin/python3 /home/pi/Documents/Fork_BookManager.py
Saved the script and waited. Nothing happend.
To summerize I have:
Made sure that the Fork_BookManager.py is executable
Verified that crontab is working
Updated path from env into the crontab script
which are three largest cause of experiencing difficulties in making crontab to work according to this guide
https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working/24527#24527
Still it is not working. What have I missed? Is there a better practice to achieve what I want, namely to execute the python script once a minute without doing it directly from the python script itself such as a while loop with a time.sleep(60)
ADDITIONAL INFORMATION
I even tried to expand the path in crontab script by
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/bin:/home/pi/Documents
in an attempt to specify in which folder the python3 and Fork_BookManager.py is located. Still no luck.

how to schedule .sh script on linux server?

I'm new to linux, and I have linux server with script ftp.sh which transfers files to windows server.
I have tried to schedule it at 06:15 every day via crontab command in PUTTY/winSCP
15 06 * * * sh ftp.sh
and get this error message:
command not found
I have also tried to schedule it from a Windows batch file with
start folderName\putty.exe -ssh root#10.*.*.* 22 -pw "password"
It succeeds with the login, but can't execute navigation to /opt/dan/scripts or execute the sh ftp.sh command in it.
What am I doing wrong? Is there another way I havn't thought about?
sh cannot execute ftp.sh since ftp.sh is propably not in the PATH environment variable. If you write the full path to ftp.sh you may succeed.
15 06 * * * sh /the/path/to/ftp.sh
Or depending on your setup
15 06 * * * /path/to/sh /the/path/to/ftp.sh
Or
15 06 * * * /the/path/to/ftp.sh
Your OS doesn't seem to recognise 'sh' as a command, as stated here:
command not found
This usually means it's not installed on the particular flavour of Linux you're using. You should use a different file interpreter, or if you're sure that 'sh' is installed then you should use the absolute path to that program in the cron command.
If you type the following in to a terminal session, it should bring up the full path:
whereis sh
Or alternatively:
which sh
Read more about the PATH variable. It is not the same in interactive shells (they usually set it in your ~/.bashrc) and in cron jobs.
So specify an absolute path for your ftp.sh shell script in your crontab and start that script with a shebang like #!/bin/sh
BTW, consider switching to Linux entirely (e.g. by installing some Linux distribution on your laptop)

Crontab absolute path not working

I have a script to backup my database at /home/<user>/bin/dbbackup. The script is executable by all users, and owned by me. The files /etc/cron.allow and /etc/cron.deny do not exist.
In my crontab I have the following lines (including a new blank line after the last line of code):
#reboot /home/<user>/.dropbox-dist/dropboxd
30 2 * * * bash /home/<user>/bin/dbbackup
However, cron is not running my dbbackup script. When I run a manual test of the script it works. When I run this test on the command line: * * * * * /bin/echo "cron works" >> ~/file I get the following error:
No command 'dbbackup' found, did you mean:
Command 'dvbackup' from package 'dvbackup' (universe)
Command 'tdbbackup' from package 'tdb-tools' (main)
dbbackup: command not found
My server is running Ubuntu Trusty. Any help please?
As the comments noted, it appears that amiga_os needed remove the reference to bash in the line.
30 2 * * * bash /home/<user>/bin/dbbackup
Should be.
30 2 * * * /home/<user>/bin/dbbackup
I usually just call scripts from their path and use "#!/bin/bash" (or wherever your bash lives) as the first line of the script. It appears the amiga_os had already done this, which is good. I don't like putting sentences into cron because it makes me nervous.
I think it was a path issue as cron executes as the user but does not read the bash profile and therefore does not work exactly like it would under your shell as it might not have access to your $PATH.

wmctrl not working in crontab

I'm using Linux (Ubuntu). I use wmctrl to make the firefox window always on top. And it worked FINE when I run the shell on a terminal.
Here is my shell code (say that it was /usr/app/keepfront.sh):
#!/bin/bash
WINTITLE="Mozilla Firefox" # Main Firefox window has this in titlebar
PROGNAME="firefox mywebsite --sync" #run the firefox program
#Use wmctrl to list all windows, count how many contain WINTITLE
WINCOUNT=wmctrl -l | grep -c "$WINTITLE"
if [ $WINCOUNT != 0 ]
then
wmctrl -a "$WINTITLE" # If it exists, bring window to front
else
$PROGNAME & # Otherwise, just launch ff
fi
exit 0
I would like to use crontab to run the shell every 1 minute. Crontab DID run the shell (I wrote some echos), but nothing happened.
Here is my crontab code:
*/1 * * * * /usr/app/keepfront.sh
Anyone know WHY? How to solve this?
cron jobs do not have access to your environment variables, although they are owned by the user they do not run in that user's full desktop environment. In this case your script does not know about your DISPLAY environment variable. To retrieve information and to make changes wmctrl needs to know which DISPLAY to use.
To do what you want to do, all you need is to set the DISPLAY environment variable in your script before any calls to wmctrl. Assuming you only have 1 monitor the line below should fix your problem (my test worked fine). If you have more than 1 monitor then just use echo $DISPLAY on the command line to help you configure the command for your various monitors.
# Add to your script before any calls to wmctrl.
export DISPLAY=:0
Some other things to note:
If you have more than one 'Mozilla Firefox' window open then your code will only bring the first one encountered by wmctrl to the top, this will be the one that was opened first because wmctrl looks through windows from oldest to newest.
I have not tested the launching of Firefox aspect of your script, genarally speaking I would have thought doing this is a bad idea because Firefox will also use environment variables which won't be set when running the script from crontab. You could find a list of all the environment variables that Firefox uses and then manually set them in the script...
You do not need the */1 bit in your crontab line, just use: * * * * * /usr/app/keepfront.sh to run something every minute.
You may wish to add some environment variables to the top of your crontab file - many people do for a variety of reasons. For instance the PATH in your crontab file probably won't be the same as your user PATH and your LANG variable won't be set either, this can stop regular expressions used in scripts called by cron from working. I have the following set at the top of my crontab file, like this:
# These are the basic paths, mine also includes my own scripts path.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Note: LANG allows grep regexes to work properly in called scripts.
LANG=en_GB.UTF-8
Hint: type echo $PATH and echo $LANG to get your current settings.
Typing env on your command line will show you all your environment variables, to see how limited those available to cron are, add this line to your crontab, don't forget to change the path I've used and to remove the line after it has run.
* * * * * env > /home/user/EnvOutputFromCrontab
Hope this helps.
One part of your problem is that this line doesn't do what you think it does:
WINCOUNT=wmctrl -l | grep -c "$WINTITLE"
It runs the command -l (which probably doesn't exist) with WINCOUNT=wmctrl as one of its environment variables.
You probably intended to write:
WINCOUNT=$(wmctrl -l | grep -c "$WINTITLE")
The other part of your problem may be that wmctrl and firefox don't work correctly when run without a terminal, as crontab runs its jobs without a terminal. I've not tried running firefox from crontab, and I can't think of anything much more annoying than having Firefox jump to the foreground every minute (OK; I can think of some things about equally annoying, but the concept doesn't bear thinking about).

crontab doesnt work in linux

My Linux version is red hat enterprise linux server release 5.3 tikanga
i have schedule crontab as below
1 * * * * /usr/testjob.sh 2>&1 >> /usr/result.txt
crontab job not running on scheduled time...
Please suggest..
Try this at first.
* * * * * /usr/testjob.sh
Then you may received a mail for every minutes. Check the error output.
Sometimes, it may caused by your default shell is just sh instead of bash.
So, maybe ">>" is not supported.
You should check do you have /usr permission when you want to write into it.
As said by +Shawn Chin, if you want to run your command only once, the at command is your friend.
If you want to run your command repeatedly, then you are right to use the cron framework. The manual page explaining the fields of the crontab may be obtained with the following command:
$ man -s 5 crontab
You appear to be in an Indian time-zone (IST). You may have to specify that into the crontab. For instance, using the 'crontab -e' command (to save and quit, type 'ESC-wq', as the editor is VI by default):
#
CRON_TZ=IST
# run at 06:33 (am), every day
33 06 * * * /usr/testjob.sh >> /usr/result.txt 2>&1
Note that '2>&1' should be placed AFTER '>> /usr/result.txt', not before.
just to mention it and make sure
NOTE: Each cron table entry must have a trailing line break in order
for the cron table entry to be recognized.

Resources