Script run from crontab can't use xvkbd or xdotool - linux

I'd like to refresh firefox automatically after 2 hour using simple bash script.
I've got:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
xvkbd -window Firefox -text "\Cr";
exit 0
and I'd like to run it using crontab -e. I've added task but nothing happens.
Everything is without any problem when I run this script with terminal.
I also tried xdotool in my simply script.
I'm not interested in firefox add-ons like "reload every" or "Tab auto reload", becouse every restart of firefox loses setting of add-ons.
Any answer or ideas will be highly appreciated.
Thank you.

There was something wierd with xvkbd package. Finally I found another solution.
I installed MozRepl add-on for Firefox.
It creates file mozrepl#hyperstruct.net.xpi in .mozilla directory. I looked through defaults/preferences/mozrepl.js and I found pref("extensions.mozrepl.autoStart", false); "false" I change to "true". This is the way that Firefox runs add-on automatically even if I close the browser.
I also wrote simple expect script:
#!/usr/bin/expect -f
set timeout 10
spawn nc localhost 4040
expect {
"repl>" {send "BrowserReload(), repl.quit()\r"; exp_continue}
"lost connection" {puts "ERROR: lost connection"}
"No route to host" {puts "ERROR: no route to host"}
timeout {puts "ERROR: timeout"}
}
Also I created a cron task:
00 */2 * * * /root/script.exp

crontab isn't a shell script. You should read more about the format of crontab by running man 5 crontab. If that seems too daunting for you, you should search for the myriad cron tutorials on Google. For instance, when you search for "Vixie cron tutorial", the first result is Newbie: Intro to cron, which upon brief inspection is pretty helpful to get you started.
For your particular use case, put the following in your crontab. (Either pasting it into the text editor that is opened by crontab -e, or saving it to a file and then do crontab FILENAME. I prefer the latter approach. You can view the contents of your current crontab by doing crontab -l. Read more about the crontab command by running man 1 crontab.)
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
00 */2 * * * xvkbd -window Firefox -text "\Cr"
This way the crond daemon should run the command xvkbd -window Firefox -text "\Cr" every two hours, at 0:00 am, 2:00 am, 4:00 am, etc. If you'd rather do it on 1:00 am, 3:00 am, etc., replace the last line by
00 1-23/2 * * * xvkbd -window Firefox -text "\Cr"
The first 00 is the minutes, so you can also replace that by, say, 30:
30 */2 * * * xvkbd -window Firefox -text "\Cr"
Then the command is run on 0:30 am, 2:30 am, etc.
As always, read the man page (man 5 crontab) or tutorial for more information. SO is not for complete tutorials.

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.

Cron run every minute ( Runs in bash but not in cron)

This have been discussed several times in previous post. I followed given advise but it does not work for me. I have two scripts which are run by the cron service every minute. To my surprise, only one runs per minute( 1st in the list below), the other fails (2nd in list below). Most surprising, when run direct from the terminal, both scripts execute fine.
Cron setup :
*/1 * * * * /home/user/Desktop/scripts/generatepattern.sh
*/1 * * * * /home/user/Desktop/scripts/getnextfile.sh
File permissions are:
-rwxr--r-- 1 user user 522 Jul 25 16:18 generatepattern.sh
-rwxr--r-- 1 user user 312 Jul 25 23:02 getnextfile.sh
The code for the non-schedulable( not running in cron ) is :
#!/bin/bash
#Generate a file to be used for the search
cd /home/user/Desktop/scripts
no=`cat filecount.txt`
if test $no -lt 20
then
#echo "echo less"
#echo $no
expr `cat filecount.txt` + 1 >filecount.txt
fi
In the last line you wrote cat filecount.txt instead of cat /home/user/Desktop/scripts/filecount.txt
I discovered the main issue was that the new cron settings only get used when the vi editor gets closed. Changes have to be made on the editor and a :wq command issued so that the new settings get installed. Just issuing :w command does not work since no install happens(this was my mistake). I realised this after issuing :wq command on vi and the following output displayed :-
# crontab -e
crontab: installing new crontab
Thanks to all other suggestions made.

Linux bash shell script output is different from cronjob vs manually running the script

I wrote a linux bash shell script which works fine except the output when I run it manually is different than when I run it from a cronjob.
The particular command is lftp:
lftp -e "lcd $outgoingpathlocal;mput -O $incomingpathremote *.CSV;exit" -u $FTPUSERNAME,$FTPPASSWORD $FTPSERVER >> ${SCRIPTLOGFILE} 2>&1
When I run the script manually, the ${SCRIPTLOGFILE} contains a lot of info such as how many files/bytes/etc transferred. But when I run the same script from a cronjob there is no output unless there was an error (such as could not connect). I have tried various terminal output configurations but none work for this lftp command. Suggestions?
It's worth reading this:
crontab PATH and USER
In particular, cron won't set the same environment variables you're used to an interactive shell.
You might want to wrap your entire cron job up in a script, and then you can, for example, temporarily add some code like export >> scriptenvironment.txt and see what the difference is between the cron invoked script and the interactively invoked script.
Try man 5 crontab for details.
Once you know what envrionment variables you need for your script to run, you can set them in the crontab as necessary, or source at the start of your own script.
EXAMPLE CRON FILE
# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

Using Cron to open and visit URL's

I've been working on a script to run from /etc/cron.daily to run everyday at 5:00 AM, script works but it doesn't seem to run after I've set all the right executive permissions.
My script is using XDG-OPEN to open up a website in a web browser, but even after setting permissions to execute, ie; chmod +x it doesn't run like it's suppose to. because in my own crontab ->> crontab -e I've made my cron job scheduled to run at 5:00AM everyday linking to the script inside of /etc/cron.daily
here's my script that I use so get clear picture how it's setup;
#!/bin/sh
LOG=/var/log/website_open.log
DATE=`date +'%a, %e %b %T %p'`
echo "*** Website opened on [ $DATE ]***" >> $LOG
echo " " >> $LOG
xdg-open http://example.com | grep -v "Created new window in existing browser session." >> $LOG
When I check the log it says it executed on which date, but it's not been doing what it's suppose to. So the logs would say this
*** Website opened on [ Mon, 24 Nov 01:04:41 AM ] ***
but it didn't really execute it properly. I did do run-parts -v to check if it worked alright, and it does, cuz it did execute properly and opened up the website after running that, but the problem is Cron isn't running it like when I tested it with run-parts -v for some reason. It's like it's being ignored, there doesn't seem to be a permission issue, because if it did it would of shown up in the logs like "Google Chrome can not run as Root" since that's my default browser to run as.
I don't believe there is anything wrong with my script since xdg-open http://example.com runs as expected from the script when tested, and in my crontab when I do crontab -e I have it set like this;
0 0 5 1/1 * ? * /etc/cron.daily/example_com
but still doesn't execute as it should be. I have thought about whether I should just execute the script from my home directory and using it with crontab, but I don't really like how crontab works since it only creates a temporary file like this;
/tmp/crontab.CjOKO9/crontab
and I just want it to be in an actual script of it's own it just runs from instead of being in a temporary file, not sure if that's entirely possible since I want it to run as my user account name and not root.
For some reason xdg-open doesn't work correctly when used in association with crontab.
You can tweak your way around to get the job done by using any of the following commands :
chromium-browser http://www.google.com
or
firefox http://www.google.com
based on your browser.

Shell script to log server checks runs manually, but not from cron

I'm using a basic shell script to log the results of top, netstat, ps and free every minute.
This is the script:
/scripts/logtop:
TERM=vt100
export TERM
time=$(date)
min=${time:14:2}
top -b -n 1 > /var/log/systemCheckLogs/$min
netstat -an >> /var/log/systemCheckLogs/$min
ps aux >> /var/log/systemCheckLogs/$min
free >> /var/log/systemCheckLogs/$min
echo "Message Content: $min" | mail -s "Ran System Check script" email#domain.com
exit 0
When I run this script directly it works fine. It creates the files and puts them in /var/log/systemCheckLogs/ and then sends me an email.
I can't, however, get it to work when trying to get cron to do it every minute.
I tried putting it in /var/spool/cron/root like so:
* * * * * /scripts/logtop > /dev/null 2>&1
and it never executes
I also tried putting it in /var/spool/cron/myservername and also like so:
* * * * * /scripts/logtop > /dev/null 2>&1
it'll run every minute, but nothing gets created in systemCheckLogs.
Is there a reason it works when I run it but not when cron runs it?
Also, here's what the permissions look like:
-rwxrwxrwx 1 root root 326 Jul 21 01:53 logtop
drwxr-xr-x 2 root root 4096 Jul 21 01:51 systemCheckLogs
Normally crontabs are kept in "/var/spool/cron/crontabs/". Also, normally, you update it with the crontab command as this HUPs crond after you're done and it'll make sure the file gets in the correct place.
Are you using the crontab command to create the cron entry? crontab to import a file directly. crontab -e to edit the current crontab with $EDITOR.
All jobs run by cron need the interpreter listed at the top, so cron knows how to run them.
I can't tell if you just omitted that line or if it is not in your script.
For example,
#!/bin/bash
echo "Test cron jon"
When running from /var/spool/cron/root, it may be failing because cron is not configured to run for root. On linux, root cron jobs are typically run from /etc/crontab rather than from /var/spool/cron.
When running from /var/spool/cron/myservername, you probably have a permissions problem. Don't redirect the error to /dev/null -- capture them and examine.
Something else to be aware of, cron doesn't initialize the full run environment, which can sometimes mean you can run it just fine from a fully logged-in shell, but it doesn't behave the same from cron.
In the case of above, you don't have a "#!/bin/shell" up top in your script. If root is configured to use something like a regular bourne shell or cshell, the syntax you use to populate your variables will not work. This would explain why it would run, but not populate your files. So if you need it to be ksh, "#!/bin/ksh". It's generally best not to trust the environment to keep these things sane. If you need your profile run the do a ". ~/.profile" up front as well. Or a quick and dirty way to get your relatively full env is to do it from su as such "* * * * * su - root -c "/path/to/script" > /dev/null 2>&1
Just some things I've picked up over the years. You're definitely expecting a ksh based on your syntax, so you might want to be sure it's using it.
Thanks for the tips... used a little bit of each answer to get to the bottom of this.
I did have the interpreter at the top (wasn't shown here), but may have been wrong.
Am using #!/bin/bash now and that works.
Also had to tinker with the permissions of the directory the log files are being dumped in to get things working.

Resources