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
Related
I am trying to schedule the execution of a shell-script with the Linux tool "at".
The shell script (video.sh) looks like this:
#!/bin/sh
/usr/bin/vlc /home/x/video.mkv
The "at" command:
at -f /home/x/video.sh -t 201411052225
When the time arrives, nothing happens.
I can execute the shell-script just fine via console or by rightclicking - Execute. VLC starts like it is supposed to. If I change the script to e.g. something simple like
#!/bin/sh
touch something.txt
it works just fine.
Any ideas, why "at" will not properly execute a script that starts a graphical program? How can I make it work?
You're trying to run an X command (a graphical program) at a scheduled time. This will be extremely difficult, and quite fragile, because the script won't have access to the X server.
At the very least, you will need to set DISPLAY to the right value, but even then, I suspect you will have issues with authorisation to use the X screen.
Try setting it to :0.0 and see if that works. But if you're logged out, or the screensaver's on, or any number of other things...
(Also, redirect vlc's stdout and stderr to a file so that you can see what went wrong.)
Your best bet might be to try something like xuserrun.
I suspect that atd is not running. You have to start the atd daemon before (and to set DISPLAY variable like chiastic-security said) ;)
You can test if atd is running with
pidof atd &>/dev/null && echo 'ATD started' || echo >&2 'ATD not started
Your vlc command should be :
DISPLAY=:0 /usr/bin/vlc /home/x/video.mkv
(Default display)
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"
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).
Hi I am really new in Linux:D
I made a crontab program which is supposed to print current time in console every 3 minutes.
What I did is below.
I made a crontab. In terminal, command
"crontab -e" and add a phrase "*/3 * * * * /home/user/a.out"
a.out is a result file of "gcc WowCron.c".
Code is below.
int main (int argc, char* argv[]){
time_t now;
time(&now);
printf("this is what we call cron does: %s\n", ctime(&now));
return 0;
}
and it works wonderfully when run individually.
Then I ran a "service cron restart" command in terminal.
Now when I command "crontab -l", I can see the messages what I wrote in crontab.
The problem is somehow I think it works, but never prints time message.
Q. How can I make this print time every 3 minutes?
Cron triggers a new process to start in the background. You configure it through a terminal (which is a process) but it has nothing to do with that terminal otherwise. Each process has it's own STDOUT, STDIN, STDERR so as the cron tasks is on a new process it won't print to your terminal process' STDOUT
As tripleee says if you'd like it to print syslog is a good place to go, or you can make it append to a file of your choice.
If you just want the program to run at a time interval in a terminal then a Shell Script is probably a better option:
while :
do
date
sleep 180
done
Or you can replace the "date" function with "./a.out" and run it from the same directory
The standard output from a cron job does not end up on the console. Try using the syslog facility.
Alternatively, if you don't need to integrate this into a larger C program of your own, use the logger command.
*/3 * * * * logger Still here ...
(The system log already includes a time stamp.)
Any standard output and standard error from a cron job ends up being sent by email to the job owner. Maybe you should examine your mailbox, or maybe your email is not working properly?
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