LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-07-2010, 09:16 AM   #1
bwilhite
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Rep: Reputation: 0
Custom Services start-stop question


Here is what I would like to do:
1) Have a service I've written start up at login
2) The service will run continuously until logout
3) At logout the service will perform some shutdown code such as writing out a file, before actually shutting down

My code is currently written in javascript to run under v8, but I wouldn't mind moving over to a different language if needed (it's all just an expirement).

The thing I'm having a hard time getting my brain around is: how does the OS signal my long-running program and re-enter into its code? Part of my problem, I'm sure, is that I know how to do this sort of thing under Windows, but I think that I'm might be missing some key concepts under linux. I've already read up on init.d scripts, run-levels and such, but I'm still not able to answer this question.

I'm hoping someone can help me pull the pieces together.
 
Old 02-07-2010, 10:56 AM   #2
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 66
Are you trying to write a daemon? The init scripts are just small scripts that perform operations (start,stop,restart,...) on the daemon. The daemon itself is going to be located in /bin /sbin /usr/bin or /usr/sbin...

If you write an initscript, you can call your daemon with an & at the end of it.... I'm not sure how to actually daemonize a process internally though.

btw, it might help to know what distribution you're using and what your service is supposed to be doing. Also, by login/logout, could you be more specific? Do you mean when the computer turns on off, when a user logs in/out,....?
 
Old 02-07-2010, 11:10 AM   #3
bwilhite
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Meson View Post
Are you trying to write a daemon? The init scripts are just small scripts that perform operations (start,stop,restart,...) on the daemon. The daemon itself is going to be located in /bin /sbin /usr/bin or /usr/sbin...

If you write an initscript, you can call your daemon with an & at the end of it.... I'm not sure how to actually daemonize a process internally though.
Yes, I believe that's the crux, how to daemonize the process internally. I had pretty much surmised that the init scripts themselves are basically just calling the program itself with different parameters. Somehow though, there is a way, after all apache does this. Any ideas where to find out how to do this?

Btw, I'm on ubuntu 8.10 and also a 9.10 box. The program I'm working on is a very basic wallpaper slideshow, so by login/logout I mean when a user actually logs in and out. On login I wish my program to start running and on logout I wish for it to do some cleanup before shutting down.
 
Old 02-07-2010, 07:20 PM   #4
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 66
Did you mean you are right in Java? (You said Javascript). I do not know how to daemonize a program. For what it's worth, I'm sure there are wallpapare rotation solutions already out there.

What you are supposed to do to start a program when a user logs in is make a .desktop file for it and put it in ~/.config/autostart. Then you won't have to worry about daemonizing.
 
Old 02-08-2010, 05:51 AM   #5
bwilhite
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Meson View Post
Did you mean you are right in Java? (You said Javascript). I do not know how to daemonize a program. For what it's worth, I'm sure there are wallpapare rotation solutions already out there.

What you are supposed to do to start a program when a user logs in is make a .desktop file for it and put it in ~/.config/autostart. Then you won't have to worry about daemonizing.
No, I did mean javascript. I won't get into the details of how I'm accomplishing that...what is bugging me, though, is that I haven't really seen anyway to write this type of program under linux, in any language, although I'm sure one must exist. I haven't yet looked really hard at other languages, though, so maybe I should.
 
Old 02-08-2010, 06:05 AM   #6
eyemole80
Member
 
Registered: Jun 2004
Distribution: Fedora Core 1
Posts: 47
Blog Entries: 1

Rep: Reputation: 16
Wow , have not heard till now that javascript can also be used in system automation.
 
Old 02-08-2010, 09:01 AM   #7
bwilhite
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by eyemole80 View Post
Wow , have not heard till now that javascript can also be used in system automation.
For those who might be curious, I'm doing it by using v8cgi server-side javascript.
 
Old 02-08-2010, 09:08 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Use Gnome's autostart facility and configure a command line calling your program (with whatever command line arguments are necessary) ending in &. The & tells the shell (that Gnome runs to execute the command line) to put your program in the background.

Last edited by catkin; 02-08-2010 at 09:09 AM. Reason: Make better sense
 
Old 02-08-2010, 12:40 PM   #9
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 66
Quote:
Originally Posted by catkin View Post
Use Gnome's autostart facility and configure a command line calling your program (with whatever command line arguments are necessary) ending in &. The & tells the shell (that Gnome runs to execute the command line) to put your program in the background.
He doesn't need an & if he uses Gnome's autostart.

For example, lets say you write a bash script that appends the date to the end of some arbitrary file. We'll call this $HOME/bin/timeprog.sh
Code:
#!/bin/bash

echo START

while [ -n "$DISPLAY" ]; do
	echo -n "<$DISPLAY>: " >> $HOME/Desktop/timefile
	date >> $HOME/Desktop/timefile
	sleep 5
done

echo STOP
echo
Then you create the desktop file, we'll call it $HOME/.config/autostart/time.desktop:
Code:
[Desktop Entry]
Type=Application
#Terminal=true
Exec=/home/matt/bin/timeprog.sh
Hidden=false
X-GNOME-Autostart-enabled=true
Name=Time Prog
Comment=A trivial program
Here is the output of the program after I log in, log out, and log back in (/home/matt/Desktop/timefile):
Code:
START
<:0.0>: Mon Feb  8 13:32:43 EST 2010
<:0.0>: Mon Feb  8 13:32:48 EST 2010
<:0.0>: Mon Feb  8 13:32:53 EST 2010
<:0.0>: Mon Feb  8 13:32:58 EST 2010
<:0.0>: Mon Feb  8 13:33:03 EST 2010
<:0.0>: Mon Feb  8 13:33:08 EST 2010
<:0.0>: Mon Feb  8 13:33:13 EST 2010
<:0.0>: Mon Feb  8 13:33:18 EST 2010
<:0.0>: Mon Feb  8 13:33:23 EST 2010
<:0.0>: Mon Feb  8 13:33:28 EST 2010
<:0.0>: Mon Feb  8 13:33:33 EST 2010
<:0.0>: Mon Feb  8 13:33:38 EST 2010
<:0.0>: Mon Feb  8 13:33:43 EST 2010
<:0.0>: Mon Feb  8 13:33:47 EST 2010
STOP

START
<:0.0>: Mon Feb  8 13:33:48 EST 2010
<:0.0>: Mon Feb  8 13:33:52 EST 2010
<:0.0>: Mon Feb  8 13:33:53 EST 2010
<:0.0>: Mon Feb  8 13:33:57 EST 2010
<:0.0>: Mon Feb  8 13:33:58 EST 2010
<:0.0>: Mon Feb  8 13:34:02 EST 2010
<:0.0>: Mon Feb  8 13:34:03 EST 2010
<:0.0>: Mon Feb  8 13:34:07 EST 2010
<:0.0>: Mon Feb  8 13:34:08 EST 2010
<:0.0>: Mon Feb  8 13:34:12 EST 2010
<:0.0>: Mon Feb  8 13:34:13 EST 2010
<:0.0>: Mon Feb  8 13:34:17 EST 2010
<:0.0>: Mon Feb  8 13:34:18 EST 2010

Here is what is running while I'm logged in to a gnome session:
Code:
root       /usr/sbin/gdm-binary -nodaemon
root         /usr/lib/gdm/gdm-simple-slave --display-id /org/gnome/DisplayManager/Display1
root           /usr/bin/Xorg :0 -br -verbose -auth /var/run/gdm/auth-for-gdm-1My6lw/database -nolisten tcp
root           /usr/lib/gdm/gdm-session-worker
matt             gnome-session
matt               /usr/bin/ssh-agent -- gnome-session
matt               metacity
matt               gnome-panel
matt               nautilus
matt               keepassx
matt               /bin/bash /home/matt/bin/timeprog.sh
matt                 sleep 5
matt               nm-applet --sm-disable
matt               /usr/lib/gnome-disk-utility/gdu-notification-daemon
matt               gnome-power-manager
matt               /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1

And here is what is running after I've logged out:
Code:
root       /usr/sbin/gdm-binary -nodaemon
root         /usr/lib/gdm/gdm-simple-slave --display-id /org/gnome/DisplayManager/Display1
root           /usr/bin/Xorg :0 -br -verbose -auth /var/run/gdm/auth-for-gdm-MBK9Ad/database -nolisten tcp
gdm            /usr/bin/gnome-session --autostart=/usr/share/gdm/autostart/LoginWindow/
gdm              metacity
gdm              /usr/lib/gdm/gdm-simple-greeter
gdm              gnome-power-manager
gdm              /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1
root           /usr/lib/gdm/gdm-session-worker
 
  


Reply

Tags
services


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
start, stop services johnkalikavunkal Solaris / OpenSolaris 1 07-14-2008 04:43 AM
Start/Stop Services Verbal Kint Linux - General 4 06-21-2007 06:56 AM
stop and start services hoover93 Suse/Novell 5 12-29-2004 02:48 PM
Gentoo: start / stop services Adony Linux - Newbie 5 12-10-2004 09:39 AM
start and stop services log Linux - General 7 03-21-2003 08:29 PM


All times are GMT -5. The time now is 05:27 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration