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?
Related
I have a program written in python,my program scrapes a value from some financial website every minute and pushes that value into my DB.My program takes like 1 or maximum 1.5 seconds to do this job. I have set a cron job to call my program every minute. I need to run my program in this way everyday from 09AM to 04PM. Now sometimes I may have to stop my program to kill the program at any time between 09AM to 4PM. How can I do this?
According to this link
I tried ps -o pid,sess,cmd afx | grep -A20 "cron$" and I was unable to find my program in the list since it completes it's work in seconds.
Referring to this I tried /etc/init.d/cron stop and pkill cron which kills all cron jobs which I don't want. I am running this cron in ubuntu linux .Any help with this would be appreciated.
Modify the program so it runs only if a particular file exists. Remove the file if you need to stop the program. (Or have it run only if the file doesn't exist, and touch the file to stop the program.)
If you're not able to modify the program, you can execute a shell if statement as a cron command.
simply rename the script name, so it will not be executed
Is it feasible to include a date time check within the program? And it won't run if before 9AM or after 4PM?
The following should also work:
* 9-15 * * * script.sh
0 16 * * * script.sh
To switch: perhaps have it read from a config/detect the presence of a file (acting as a flag) to determine whether or not to run.
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
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.
I have a script which I need to run daily at 01.00 am of every month. This is what I did with cron tab to make it happen
#housekeeping
0 1 1 * * sh /product/abc/tools/housekeeper.sh -t ABC
How can I check whether this script is running? Do I have to wait till it runs or is there a process running for this script so that I can confirm it is running?
The usual approach to test crontab scripts follows these guidelines:
Set the time pattern to "ten minutes from now". Wait ten minutes. Check cron's logfile and the log files of your script/app for errors.
Set the desired time pattern
This way, you can make sure that the script works. If something breaks, it must be because of the time pattern.
You should also redirect all output of the script to a log file. cron will send a mail with any output that the script produces but that's rarely desired.
If you always add an entry like "${timestamp} Started" to the log, you can check at your leisure that the script worked as expected.
If you want both, then you must make your script/app write the log file itself (i.e. without output redirection in the crontab) and print any errors to stdout; this will then make cron send an email.
Simple thing I use is to append something to a particular file before and after the desired command, like
*/2 * * * * (echo "Starting" >> ~/monitorCron.log; <Actual operation>; echo "End" >> ~/monitorCron.log;) > ~/cron.log 2>&1
This runs every 2 minutes, and appends the strings to ~/monitorCron.log.
This is the same thing I do in programming, writing a print statement where-ever and when-ever I get a doubt.
I'm using Ubuntu Linux 10.0.4. I want to run a script every 6 hours, every day. When I issue sudo crontab -e, I see:
# m h dom mon dow command
* 00,06,12,18 * * * /opt/scripts/selenium/run_nis_inf_tests.sh
However, I'm not seeing the expected outcome from my script, and I'm not even sure if its running. Is there a way to test, short of waiting until the specified time, that the script is running properly. Or, how can I view the errors the script is generating? - Dave
You can update the MAILTO variable to your email address, and cron should email you any STDOUT and STDERR output. Also check your syslog file /var/log/messages to see if the script is being executed by cron.
-Tony
Cron should mail it results so it looks like you have a problem.
Here, it seems you are missing a user to run the script as :
00,06,12,18 * * * user_name /opt/scripts/selenium/run_nis_inf_tests.sh
replace user_name by the name of the user the script needs to be run by, verify permissions of "run_nis_inf_tests.sh" and you should be ok.