Schedule a zenity message on linux - linux

I want to create a simple reminder on linux at a certain time everyday. I am using crontab to schedule running the reminder script, while in the script I am using zenity to display a simple dialog box with a question.
In more details, I have the following script reminder.sh:
#!/bin/bash
zenity --question --text="question?"
Then, using crontab -e, I add the following task to run every single minute (just for testing):
* * * * * /path/to/reminder.sh
But the dialog box doesn't appear. I added some other commands to the script and I confirmed that the script runs every minute, but the dialog box still doesn't appear!
Any hints?

I did find the solution. When I did echo of the $DISPLAY in the same script, it has nothing in it. So, I've set it to my machine display. But this didn't work till I added xhost + to the script.
The final script would look like:
#!/bin/bash
xhost +
/user/bin/zenity --question --text="question?" --display="myMachine:0.0"

Related

Making cronjob from AppleScript

I am trying make a crontab run an apple script.
My problem is the script doesn't seem to run. i dont get any error messages or anything the script just doesn't run.
The my apple script just opens the mac calculator. As follows:
tell application "System Events"
get name of every process whose name is "Calculator"
if result is not {} then
tell application "Calculator"
quit
end tell
else
tell application "Calculator"
activate
end tell
end if
end tell
This script runs fine and opens the calculator no problem when I click run button in the script editor window.
I saved this file as calculator_script on my desktop, it is a .scpt file.
To run the cronjob i open terminal and type:
crontab -e
i
23 16 * * * cd Desktop && calculator_script.scpt
press esc
:wq
it says a new cronjob is created but but when the time comes that i specified in the cronjob expression nothing happens.
I can't see where I've gone wrong.
I know the script works because it opens the calculator, so it must be something to do with the cronjob. I make sure that I put the time a couple of minutes into the future in the cronjob expression.
When type crontab -l i see my cronjob in the list. it just never runs for some reason.
iactually i solved it, i needed to use osascript to run the script. so it ended up being:
59 16 * * * cd desktop && osascript calculator_script.scpt

Linux notify script

I want to write a script in Linux which will notify me every half an hour by some alert message or something, whenever I'm logged onto the system. how can I do something like that on OS level?? It's a mixture of cronjob and javascipt alert message. how can I do it?
I found a solution :-
import sys
import pynotify
if __name__ == "__main__":
if not pynotify.init("icon-summary-body"):
sys.exit(1)
n = pynotify.Notification("Heading","content","notification-message-im")
n.show()
and then run a cronjob
Do you need the notification in a window environment or just a notification in some way? The easiest would probably be to use cronjobs MAILTO tag to define a recipient to the output of the executed script.
Like so:
MAILTO=email#example.com
* * * * bash ~/test.sh
Alternatively, you could just make the computer beep in different patterns at different times using cron. On Ubuntu at least, there's a utility called beep that should make this very easy.
A simple way for having a script writing you a message every half hour on your shell would be, in C :
void main()
{
while (1) {
sleep(1800);
printf("My Message \n"); }
}
Compiling this using gcc
gcc myfile -o my_script && chmod +x my_script
Open your main terminal & write :
./my_script &
You'll keep working on your shell, and every half hour, your message will pop up; you can use stringformats to include beeps in the printf for example, or just go do whatever you'd like instead.
Edit: For printing a pop-up in your system, you'll need to use kdialog -like tools, i'll be printing an example with kdialog, since that is working on my system, but things like gtk-dialog-info or else might work aswell :
#!/bin/sh
while :
do
kdialog --msgbox "MyMessage";
sleep 1800;
done
And doing in the shell :
sh myscript.sh &
I think what you're looking for is a mixture of cron and write. Keep in mind that though it does allow you to send messages to terminals, receiving a message can mess things up in fullscreen programs (e.g vim or emacs).
EDIT: if you want a window to pop up, I recommend xmessage or zenity
Source: http://ubuntuforums.org/showthread.php?t=876618
Put the following in your user-level crontab. You can open it by typing crontab -e in your terminal.
30 * * * * DISPLAY=:0.0 notify-send "Red alert" "All personnel must evacuate"
notify-send will display a GUI notification. It needs the DISPLAY env variable to be correctly set to the display you're logged on (most likely :0.0). (For some GUI apps, you might need additional env variables such as DBUS_SESSION_BUS_ADDRESS).
For testing purposes, replace 30 with * to get that message every minute, rather than each 30th minute.
More on:
https://help.ubuntu.com/community/CronHowto

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

when linux system calls scripts some commands don't work ( cron / if-up.d )

Hi I'm trying to run a script that calls xclip in order to have a string ready to paste when i connect to the internet.
I have a script /etc/network/if-up.d/script that does execute when connecting (i make him post a date in a file succesfuly ) but the xclip instruction seems not to work, there's nothing to paste. If i call this script manually by typing /etc/network/if-up.d/script in a console it works perfectly.
If i try to launch a zenity message it also don't appeare when connecting. Again if i do it by hand it appeares.
Then I have a expect script that calls matlab (console mode), if I execute it manually it works but if i call it from cron it freezees when calling the script.
It's driving me crasy since it seems that only certain commands in a script can be executed when the system calls them automaticaly.
I'v tryed to call the instructions with nohup instruction & but still misses
This is working as designed, you search around and will see compliated ways to resolve this issue, or you can use xmessage as I describe here: Using Zenity in a root incron job to display message to currently logged in user
Easy option 1: xmessage (in the script)
MSSG="/tmp/mssg-file-${RANDOM}"
echo -e " MESSAGE \n ==========\n Done with task, YEY. " > ${MSSG}
xmessage -center -file ${MSSG} -display :0.0
[[ -s ${MSSG} ]] && rm -f ${MSSG}
Easy option 2: set the DISPLAY (then should work)
export DISPLAY=:0 && /usr/bin/somedirectory/somecommand
question is answered here for cron :
http://ubuntuforums.org/archive/index.php/t-105250.html
and here for if-up network :
Bash script not working properly when run automatically

Resources