I'm studying how to set up ubuntu server.
Today, I learned how to use crontab on the web and I tried it in my local mac computer. (not ubuntu server)
crontab -l
* * * * * /bin/echo 'hi'
I added a very simple cron job to check if it runs correctly, but it does not run!!
I tried the following, but both do not work.
A
crontab -e
* * * * * /bin/echo 'hi'
B
crontab -e
* * * * * echo 'hi'
I'm expecting that I see 'hi' every minute on my terminal console.
What am I doing wrong?
Updated
mail
I tried mail command and I saw the list of log echoing hi.
it seems it is running but how can I see 'hi' in my terminal?
echo writes on stdout, which is normally attached to your Terminal. crontab jobs however, don't have Terminals associated with them - if you have a little think about it. What would happen if you had multiple Terminals open, how would it know which one to send your "hi" to? It doesn't.
As you are learning, it might be informative to try the following, though it is not for production quality code.
Start a Terminal, and get its associated device special file:
tty
/dev/ttys000
Now make a cronjob that writes there:
crontab -e
* * * * * echo "Hi" > /dev/ttys000
Related
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 am trying to run a shell script using cronjob after every 2 minutes. I opened my terminal then typed
crontab-e
once i execute this command i am writing my command
*/2 * * * * /home/test/test/test.sh
but i am getting an error as
E486: Pattern not found: 2 * * * *
please help as i am new to this and i don't know why it is happening.
If you give me any links and code on hwo to execute cronjob it would help.
As mentioned in comments, the following error:
E486: Pattern not found: 2 * * * *
Was caused because you were not editing properly. That is, you were saying crontab -e correctly and then you were entering in vi. Instead of going into the insert mode, you would directly type */2 * * * * /home/test/test/test.sh, which vi would try to perform as a command, which is not.
So what you have to do is to press i to enter in the write mode. And then, just then, write */2 * * * * /home/test/test/test.sh. Finally, save the file by saying :wq.
In case other problems occur in your cronjob, you may want to check the "Debugging crontab" section in https://stackoverflow.com/tags/crontab/info.
I would like to show it in more detail:
crontab -e
i
* * * * * cd path/to/your/project && command
ESC
:wq
ENTER
Please check the link in the correctly marked answer above for learning more about crontab.
I'm trying to run a script every minute with "SINC is not CRON". I've used the following crontab line
* * * * * ruby -X D:/xampp/htdocs/maker ./do.rb
and now every minute I get a new cmd window. How can I force Ruby or SINC to invoke these as background processes?
From this link:
"In these cases, you'll want to use
rubyw.exe. It is the same as ruby.exe
except that it does not provide
standard in, standard out, or standard
error, and does not launch a DOS shell
when run."
I'm not familiar with SINC, but something like this should work:
* * * * * rubyw -X D:/xampp/htdocs/maker ./do.rb
or
* * * * * rubyw.exe -X D:/xampp/htdocs/maker ./do.rb
FWIW this is similar to Python where you would use pythonw.exe.