LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-13-2008, 08:29 PM   #1
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 10
Posts: 162

Rep: Reputation: 33
How to gracefully start and stop programs from the command line?


I'm trying to use cron to start and stop X programs. I've got cron configured; it's just the commands I'm not sure of.

1) I don't want to "killall <app>". Some programs complain about unexpectedly closing, and don't save settings correctly.

2) How do I start an X program from the command line? If I run the command from an xterm, it already knows to launch it to X. But cron seems to run jobs on just a basic bash prompt, so I keep getting mail from cron about how the programs can't launch without a display.

Thanks for any help!
 
Old 02-13-2008, 09:45 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
it's a bit odd that you'd want a cron job to try to pop something up on the screen. What is it that you're trying to do? maybe there's another way of doing it?

To attempt to answer your questions
1. killall is not a bad route. It's actually diffuclt to tell a program to close unless it provides you with a mechanism to do so. This is a good idea as you wouldn't want someone unexpectedly closing an app that you're currently using. To get around the messages about improper stopping of a program, you can send signals with killall that the program may respond to more gracefully. "man kill" (as opposed to man killall) will show the available signals. Give a few a try.

2. I don't want to make this sound too dumbed down, so forgive me if I come across as condescending... it's not my intent, but: X isn't like windows in that there's no single display. An X server could be serving any number of displays - some of which aren't even attached to a monitor (or even this computer). When you launch something from outside of an X session, X has no idea where you want that thing to open - what display? what session? etc etc. Do you want it to pop up on your screen? Which screen is that? If you have active processes running on two displays, which one is the right one? I ask these questions to demonstrate that it's non-trivial. For this reason, I might take a step back & ask what you're doing to see if there's some other way... maybe you need a deamon that runs, accepts a request from cron, and responds with a window to display 0? Maybe you're just trying to run a graphical app in a non-graphical mode & you don't care if it's displayed or not?
 
Old 02-13-2008, 09:48 PM   #3
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
You need to set the DISPLAY variable, usually to :0 if you're running the xterm on the same machine, or <ipaddr>:0 if on another one.

Once you launch the xterm from the command line, it takes control of the user i/o, so you still have some loss of control to "gracefully" terminate it. This isn't different than other programs that aren't designed to communicate with the launching program.
 
Old 02-13-2008, 11:48 PM   #4
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 10
Posts: 162

Original Poster
Rep: Reputation: 33
Ah, of course. Reading the manual did the trick for problem one. "killall -s TERM <app>" works great. pkill looks interesting also.

For problem two, I mostly am wanting to stop and start Azureus. I'll probably switch to using rtorrent, but the problem itself became more interesting than the original task.

I switched out of X (ctl-alt-1), and ran "env DISPLAY=:0", and then "azureus". It didn't work. Afaik, I only have one display, and it's attached to my local monitor.
 
Old 02-14-2008, 12:23 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Using "-SIGQUIT" instead of "-SIGTERM" may be cause the program to quit more gracefully, such as closing files, etc.
 
Old 02-14-2008, 01:31 AM   #6
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 10
Posts: 162

Original Poster
Rep: Reputation: 33
Oddly, java/azureus doesn't respond to SIGQUIT.
 
Old 02-14-2008, 01:56 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Quote:
Originally Posted by djeikyb View Post
Oddly, java/azureus doesn't respond to SIGQUIT.
OK, I bet that SIGQUIT in this case would cause the window to close but remain running with an icon in the toolbox. Much like clicking the close button on an open amarok button.

According to this post, on solaris, you would use SIGHUP or SIGTERM. Also "
the Java VM uses SIGQUIT to dump java thread stacks to allow
monitoring or debugging of the Java application"http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4381444

On Linux, the VM passes SIGHUP, SIGINT, SIGQUIT & SIGTERM to the application.
 
Old 02-14-2008, 09:48 PM   #8
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
This works for me:

<Ctl-Alt-F2>
[login]
mymachine:~>export DISPLAY=:0
mymachine:~>xclock
<alt-F7>

[xclock is running on the KDE Desktop]
 
Old 02-14-2008, 10:03 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,151

Rep: Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203Reputation: 2203
Hmmm... what a confusion of ideas here! Let me toss my two-cents in...

(1) If you want to cause a program to start running "at a particular time of day," cron is the tool that you would use to do that. It's roughly equivalent to Windows' "scheduled tasks" facility.

(2) If you want a program to start when the computer is booted, look at the magical concepts of "runlevels" and "inittab." Look in directories like /etc/rc.d.

(3) If you want programs to start when you log in... look at files like .bash_login.

(4) From the command-line, appending '&' to any command will cause it to run "in the background" while you do something else.

(5) If you want a program to stop running, "if you own it, you can kill it."

(6) If you are totally confused... ... don't panic. You're accustomed to a highly-structured world (Microsoft Windows). Unix/Linux isn't like that. The same things happen, but whereas Windows is a tightly-coupled system, Unix and Linux are the opposite extreme: an extremely loosely-coupled system.

It takes some getting used to... but you know, that's what drove Messrs. Kernighan and Ritchie to get involved in this whole thing called "Unix," back in the 1970's on their PDP-8 at Bell Labs. I guess it was "a supremely counter-culture system." (But then again, "everybody knows Unix, but..." who remembers MULTICS?)
 
Old 02-14-2008, 11:35 PM   #10
djeikyb
Member
 
Registered: Nov 2005
Location: California
Distribution: ubuntu 10
Posts: 162

Original Poster
Rep: Reputation: 33
Haha, I've never been able to get MSWindow's scheduled tasks to do everything I want. Nowadays I install cygwin so I have cron and other linux utilities ; )

JWPurple-- "export DISPLAY=:0" did the trick. Apparently env is for running commands, so "env DISPLAY=:0 xclock" also works.

Everything works now. Thanks all you who've replied!
 
  


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
How to gracefully start and stop VNC? landev Linux - General 2 02-02-2008 07:06 PM
How to stop a command or start over, in a terminal royeo Linux - Newbie 6 08-12-2006 02:21 PM
Running Programs from command line sumguy231 Linux - Software 4 12-17-2004 04:10 PM
launching programs from command line in FS dertien Linux - Software 1 11-26-2004 10:27 AM
how to stop certain programs from launhing when Kde start? sirpelidor Linux - General 2 09-08-2004 01:26 AM


All times are GMT -5. The time now is 02:40 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration