Running command from cron not working with no error [duplicate] - linux

This question already has an answer here:
Jquery element.text() error "is not a function" when I use elements array
(1 answer)
Closed 4 years ago.
I am trying to create a method to change my desktop background randomly. I am using crontab to handle the change every 10 minutes.
The crontab
*/10 * * * * /usr/bin/feh --recursive --randomize --bg-fill
/home/aaron/Pictures/wallpapers/minimalist 2>&1
The syslog
syslog:Oct 20 09:20:01 skull-nuc CRON[19895]: (aaron) CMD (/usr/bin/feh --recursive --randomize --bg-fill /home/aaron/Pictures/wallpapers/minimalist 2>&1)
syslog:Oct 20 09:30:01 skull-nuc CRON[20449]: (aaron) CMD (/usr/bin/feh --recursive --randomize --bg-fill /home/aaron/Pictures/wallpapers/minimalist 2>&1)
Trouble shooting -
First I changed my shell to sh and tested the command. It works. I tested the command in bash. It works. I allow it to run from cron and nothing happens and no error is produced. It just runs every ten minutes and my background only changes when I do it manually.
I have verified
Script works alone
Script works from sh
cron service is running
cron is running the command with no discernable output
I am unsure what else to do

The cron environment will usually differ from the environment you have in an interactive shell. In this case, you should check the DISPLAY environment variable, which many X utilities use to figure out which session to connect to.
If it's not set, feh will probably fail in just the way you described.
Missing environment variables can be set directly in the command line you're using in the crontab, or you can write a wrapper script that sets up the environment, then calls feh, and then call the wrapper from cron.

Related

Can't get Crontab jobs to work at all [duplicate]

This question already has answers here:
CronJob not running
(19 answers)
Closed 4 years ago.
New to Crontab use, I am trying to get a simple bash script to run.
add_temp:
#!/bin/bash
rnd1=$RANDOM
range1=20
let "rnd1 %= $range1"
rnd2=$RANDOM
range2=50
let "rnd2 %= $range2"
echo $rnd1
echo $rnd2
cd /var/www/html
sqlite3 test.db <<EOF
INSERT INTO temps (date, temp) VALUES ($rnd1, $rnd2 );
EOF
crontab -e:
SHELL:/bin/bash
* * * * * /var/www/html/add_temp
This doesn't seem to run at all. Works fine if run manually with /var/www/html/add_temp.
My guess is that the sqlite3 is not found when the cron daemon run the script.
You could modify the script and provide the full pathname of this command. Use a terminal to display the full pathname of the command:
type sqlite3
The cron daemon which will run the script does not have the exact same environment than the bash login shell used to run manually the script.
The variable that is usually differently set is PATH.
The first step to troubleshoot this situation is to compare the environments and debug the script.
I suggest to insert these lines below, after the very first line of the script
env
set -x
Run the script manually and redirect output, from a terminal:
/var/www/html/add_temp >/var/tmp/output_m.txt 2>&1
Change the crontab line for:
* * * * * /var/www/html/add_temp >/var/tmp/output_c.txt 2>&1
Wait that the cron daemon executes the script and compare the /var/tmp/output_m.txt and /var/tmp/output_c.txt files.
When the script will be fixed, remove the 2 debug lines from the script and restore the crontab original content.

crontab is returning error

I'm trying to run some crawler with Linux crontab.
This should go to the Python environment with
pyenv shell jake-crawler
Here is my crontab -e
*/10 * * * * /home/ammt/apps/crawler/scripts/bat_start.sh
This will run every 10 minutes. This command line works fine when I type
(jake-crawler) [jake#KIBA_OM crawler]$ /home/jake/apps/crawler/scripts/bat_start.sh
[DEBUG|run.py:30] 2017-09-24 19:55:49,980 > BATCH_SN:1, COLL_SN:1, 1955 equal 0908 = False
Inside of bat_start.sh I have init.sh which changes the environment to Python.
Here is my init.sh
#!/usr/bin/env bash
export PATH="${HOME}/.pyenv/scripts:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
pyenv shell jake-crawler
This has no problem when I personally run it from command line. But when cron run it by itself, it cannot find the pyenv command.
I think that you can specify which user should run that script in cron configuration file.
So, if that script is working with your user, then define it in your cron configuration filr.
See this answer for example... https://stackoverflow.com/a/8475757/3827004.
There are two things that diferentiate when you launch an application from the terminal, and when you do from a crontab file:
the environment is not the same, at least if you don't execute your .profile script from your cron job.
You don't have access to a terminal. Cron jobs don't use a terminal, so you will not be able, for example to open /dev/tty. You will have to be very careful on how redirections are handled, as you have them all directed to your tty when running on an interactive session, but all of them will be redirected possibly to a pipe, when run from cron(8).
This makes your environment quite different and is normally a source of errors. Read crontab(1) man page for details.

Use linux command output in crontab

I made a shell script and registered to execute every 20 minutes.
Here is my crontab code.
*/20 * * * * sh /mypath/run_myprocess.sh &> /dev/pts/34
I editted code like this in order to see whether my process run correctly.
I get the result '/dev/pts/34' from tty command in terminal.
However, does anyone know how to use linux command results(in this case: /dev/pts/34)
in crontab? This is because I will use several terminal to run my tasks.
For example, in shell script, I can use linux command result in the form of $(command) such as
echo "$(date)"
directly.
Plus, if I type something on the terminal during process running with crontab, it actually gives result. For example,
Process is running........
ls
backup backup.sh Desktop Task_Folder shared_folder
[UserID] ~ #
So I guess cron jobs run correctly but in background.
Please help me to find out how can I bring cron jobs in foreground.
If you start a job on your console and background it you can then bring it to foreground. If the task is not yours or not started on your terminal then you can not.

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).

How to execute a php script every day [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cron job on Ubuntu for php
I am running and ubuntu server and wanted to run a php script every day. I have done some research and found that cron is the best way of doing this however, this is where i got stuck, a lot of the information on the internet about cron is very hard to follow and understand.
So i wanted to execute a simple php script once a day, the script i made for testing simply just deletes a record from a database, but the real script will do a lot more.
I tried setting up a task through plesk which is provided through my web host service but it didn't seem to execute when i wanted it to, i used 1 for minutes, 22 for hours, * for day, * for week and * for month and thought this would execute every day at 22:01.
I have the directories on my server:
cron.hourly
cron.daily
cron.weekly
cron.monthly
I thought i could dump i file in there and it would execute for example every day, but i'm guessing i need to make a cron script to call a php script right?
If i were to go the way of putting a file in the cron.daily folder how would i go about it?
Also if there are any steps i need to take on the php side please let me know?
Thanks a lot for your time.
There's couple of ways to setup cron job. Assuming you got shell access you could do crontab -e from console and define job there, i.e. like this:
1 22 * * * command
which would trigger command (whatever it is) at 22:01 each day (not sure why you set minutes to 1 instead of 0 though). To launch PHP script from there you would either have to install php-cli, and then invoke it that way:
1 22 * * * <path>/php -q script.php
You can also call bash script here, to setup all the stuff like paths etc and then call your php script form bash - sometimes it is simpler to do that way instead of crafting way too long command line for cron. And it's simpler to update it later. also, you could turn your php script into bash-runnable script by setting it execution bit (chmod a+x script.php) and adding shell's shebang:
#!/usr/bin/php -q
<?php
...
If your script got too many dependencies and you'd prefer to call it via web, you could use wget to mimic a browser. so your command would be:
/usr/bin/wget --delete-after --quiet --spider <URL-TO-YOUR-SCRIPT>
wget manual can be accessed by man wget or wget -h, or is on this website. Aternatively you may use HEAD tool from perl-www package - but it requires perl while wget is a standalone tool. If you use HTTPS with self signed certs, add --no-check-certificate to your invocation arguments. And you may also want to setup .htaccess and limit web access to your cron script to localhost/127.0.0.1
every minute:
* * * * * /path/script.php
every 24hours (every midnight):
0 0 * * * /path/script.php
Se this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml

Resources