LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-10-2009, 09:33 AM   #1
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Rep: Reputation: 15
Open and close a program at specific times in cron


Guys,
How can I schedule cron to open and close a program at specific times?
I just don't know how to schedule it to close automatically.

Regards
 
Old 03-10-2009, 10:28 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
The cron job that stops the process will have to search for the process by name and kill it. A better approach may be to have the start script save off the processes PID into a file and the stop process can read the pid (verify the name) and then kill it. Care should be taken killing off the process, some programs are programed to respond to a TERM signal and exit gracefully, others are not.
 
Old 03-10-2009, 01:38 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
More specifically the start-up script:

Code:
#! /bin/bash
/full/path/to/your/program > /dev/null &
echo $! > /var/tmp/myprogrampidfile.pid
exit
and shutdown:
Code:
#! /bin/bash
if [ -f /var/tmp/myprogrampidfile.pid ]
then
   /bin/kill $(cat /var/tmp/myprogrampidfile.pid)
fi
Make sure you redirect the output of your program to something else but stderr or stdout.

Use full paths everywhere.

If you run the cron job with root permissions you can store your pid file in /var/run, which is nicer.

Test the scripts outside cron first.

This is untested code!

jlinkels
 
Old 03-11-2009, 08:41 AM   #4
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 15
Thanks!
it worked
 
Old 03-11-2009, 09:09 AM   #5
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 15
I need to start rtorrent from cron. But trying to execute it directly doesn't work, so I'm using screen to execute it, like this:
0 19 * * * /usr/bin/screen -d -m /usr/bin/rtorrent

How can I retain rtorrent PID in this situation?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto-login at specific times Vorik Linux - Software 6 12-11-2007 01:04 AM
Running a program at specific times only i666an SUSE / openSUSE 4 04-02-2006 07:49 AM
Simplest way to open and close specific ports ? Natimus Linux - Security 6 04-05-2005 09:01 AM
wine crashes on 16-bit program open/close StuP Linux - Software 0 06-06-2004 01:21 AM
Logging off and locking out users at specific times Kahless Linux - Software 5 02-19-2004 01:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration