I was recently running a cron job using crontab -e and I found some strange behaviour. The following command doesn't work:
* * * * * cp /home/username/{*txt,*pdf} /home/username/test/
but the following does
* * * * * cp /home/username/*txt /home/username/test/
while both commands work in bash.
Why am I not able to use curly brackets in cron?
I suppose cron uses the sh shell to run your commands by default. sh does not support curly-brace wildcards.
IIRC, you can add to your crontab the following line:
SHELL=/bin/bash
Related
I want to read a env variable in crontab.
I tried with run printenv from crontab and it runs fine:
* * * * * BASH_ENV=/etc/profile bash -c "printenv CNE_CLUSTER_NAME >> output.txt"
I need to run a job with some operation performed by python's script. I sheduled it in crontab like this:
* * * * * BASH_ENV=/etc/profile bash -c "/usr/bin/python3 /var/cne/cluster/$(echo $(printenv CNE_CLUSTER_NAME))/scripts/setup/job.py"
But when running from crontab is var CNE_CLUSTER_NAME is empty
python3: can't open file '/var/cne/cluster//scripts/setup/job.py': [Errno 2] No such file or directory)
So, how can i do to read variable or replaced the values's variable in the string ? Please.
You don't need printenv to acces the value of CNE_CLUSTER_NAME.
* * * * * BASH_ENV=/etc/profile bash -c '/usr/bin/python3 /var/cne/cluster/$CNE_CLUSTER_NAME/scripts/setup/job.py'
$CNE_CLUSTER_NAME is passed literally, and the shell that executes the command will expand the parameter to the value found in its environment.
I'm trying to run a cron command every minute in mac os. Below is the code for my .sh file named logit.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/Users/myname/Desktop
printf "\nThis is a new line to your document" >> file.txt
I made the file executable with below command
sudo chmod +x /Users/myname/Desktop/logit.sh
Below is the code for my crontab. I need to run the command every minute to append the text to the text file to check whether cron is working properly.
* * * * * sh /Users/myname/Desktop/logit.sh
I tried the below command as well, it doesn't work
* * * * * /Users/myname/Desktop/logit.sh
However, if I give the below command in the terminal, it works fine.
sh /Users/myname/Desktop/logit.sh
What am I missing here?
The problem was with the syntax for the crontab file. Below is the code which worked for me.
* * * * * cd /Users/myname/Desktop && ./logit.sh
I am having difficulty running the below simple script from crontab:
#!/bin/bash
notify-send "battery.sh working"
The permissions of the file are rwxr-xr-x and its running fine with either of the commands bash battery.sh and sh battery.sh.
In my crontab, I have tried running it with both bash and sh, with absolute as well as local path. My current crontab looks as follows:
* * * * * /home/marpangal/battery.sh
* * * * * sh battery.sh
* * * * * bash battery.sh
* * * * * sh /home/marpangal/battery.sh
* * * * * bash /home/marpangal/battery.sh
However the cron does not execute the script and I get no message from notify-send.
notify-send needs the DBUS_SESSION_BUS_ADDRESS environment variable in order to communicate with the current desktop session. Since cron runs with an almost empty environment, this variable is not available.
But you can set it directly in your battery.sh script:
export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)
notify-send "Message from cron"
This looks up the process id of your gnome-session and extracts the DBUS_SESSION_BUS_ADDRESS (along with its value) from then gnome-sessions' environment.
Now notify-send is able to display notifications in your desktop session.
Flopps’s answer gives me a -bash: warning: command substitution: ignored null byte in input – so i tried something different:
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
notify-send "Message from cron"
i assume this is not as flexible as the original export but it works for me in my user crontab.
My crons are working normally, I tested it with simple file outputs and bash scripts, but nothing I have tried works when it comes to executing my python script.
So this is what I did.
as per usual I added a shebang to my script
#! /usr/bin/python3.5
&
chmod +x myPythonScript.py
I added it to crontab -e and tested on various ways.
examples:
* * * * * /path/to/myPythonScript.py
and just to be sure I even did the following
* * * * * python /path/to/myPythonScript.py
* * * * * python3.5 /path/to/myPythonScript.py
* * * * * /usb/bin/python /path/to/myPythonScript.py
* * * * * /usb/bin/python3.5 /path/to/myPythonScript.py
and even did */1 * * * * and other time tests.
Nothing worked, and if I write all of these outside the cron, everything works perfectly fine every time. I even changed the SHELL in the crontab -e knowing that it will not make any difference, but just to be sure, pasted the script in /etc/cron.hourly, nope, tried with /usr/bin/env python instead of the usual shebang, tried every other dumb thing, but it did not work out.
Cronjobs worked perfectly when running a bash script, I even ran a bash script to check if my python script is up and running and to notify me about that in a text log, and if its not, to restart it and also notify me about that, my text log was full of restarts and that was because it never ran the python script, so the cron was working obviously, because it ran the bash script.
When running cron, you should always write your standard out/err to a file to see whats happening. Don't trust mail because your mail server may/maynot be setup.
So, give this a try
* * * * * python /path/to/myPythonScript.py 1>/tmp/myPythonScript.out 2>/tmp/myPythonScript.err
After 1 minute check to see if the file /tmp/myPythonScript.{out,err} exist. This will give you a clear direction of what is happening.
Again, ALWAYS write your standard out/err
I wrote the following bash script named /home/pi/test.sh (that uses the ruby gem "tumblr-rb"):
#!/bin/bash
echo "starting script"
/usr/local/bin/tumblr post /home/pi/test.jpg --host=myhost.tumblr.com --credentials=/home/pi/.tumblr
echo "end of script"
If I run it from the command line, I get:
starting script
Post was successfully created! Post ID: 82238814640
end of script
And my picture gets posted to Tumblr. Then if I put the bash script in my pi user's crontab with * * * * * /home/pi/test.sh, I get the following cron output e-mailed to me:
starting script
end of script
And nothing gets posted to Tumblr. Why does nothing happen?
Cron passes a minimal set of environment variables to your jobs.
You can use a simple shell to check the environment crontab provided:
* * * * * env > /home/pi/cron_env.dat
Try to add the full path of your ruby binary to execute the ruby script.
You can also set the PATH variable in the crontab file, which will apply to all cron jobs. e.g.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
* * * * * bash -c /home/pi/test.sh