cron job not working for xwindow - bash

I have following line in crontab
*/1 * * * * xeyes
it does not show any xwindow but on the contrary
*/1 * * * * touch somefile.txt works fine
Tried to search on google but didnt get any specific answers!!

You have to tell cron where to find the X server if the command you run uses it.
So use: env DISPLAY=:0.0 xeyes or export DISPLAY=:0.0; xeyes.
Some cron implementations (Debian, Ubuntu, ...) allows to set enviroment simply in cron file.
DISPLAY=:0.0
# m h dom mon dow command
*/1 * * * * xeyes

Related

How do I activate cron job?

I edited the cron file on my Mac running catalina.
I used this code:
crontab -e
And then I added this line
* * * * * python3 ~/Documents/Scripts/script.py
My expectation is that it should run every minute but it hasn't run once.
Do i need to do something special to activate cron on this machine?
Updated my code based on the conversation in the comments to this
* * * * * /usr/local/bin/python3 ~/Documents/Scripts/script.py >>~/Documents/Scripts/script.log 2>&1

Running bash script from crontab

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.

Cron Unix dude with shell

I need make a task with cron but it doesn't work.
My code cron
*/1 * * * * /home/script.sh
does not call the script.
Try to check your path to the script. Maybe it is located in user directory.
*/1 * * * * /home/user/script.sh

Cron job with shell script

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.

Running script crontab Mac OS X

I have created a script that empties the Trash every 5 minutes. However, I am not able to run the script.
Here is the location of the script.
/Users/Ryan/Documents/trash.sh
Here is what my crontab looks like.
*/5 * * * * /Users/Ryan/Documents/trash.sh
Any ideas why it's not working? Thanks in advance.
I found the answer.
*/5 * * * * sh /Users/Ryan/Documents/trash.sh

Resources