Jump to content

Running and stopping a program at login and logout


Recommended Posts

I have written an add on to the CUPS printing system in PERL which implements a quotas scheme. However, my old method of emailing success or failure and the reason was often ignored by users so I have designed a printer monitor system which pops up a windows with these messages. However, the system requires a program running in the back ground on the local machine which the print server connects to. To get this running properly I need to run this program as the local user when they log in and to stop the program when they log out, so that when another user logs in the program can be run as them.

 

I am running Mandrake 9.0 with the GNOME desktop and the KDE login manager.

 

I know I can launch a program when a user logs in through the session part of the GNOME configuration but this leaves the program running when the user logs out. How do I stop the program from running when the user logs out?

 

Thanks

Link to comment
Share on other sites

Or a script in the menu

#!/bin/bash

init 3 &

killall /usr/bin/program

I'm not good at scripts, and someone could probably give a better example/suggestion, but it's what I'd try. :roll: Right now I'm using autologin with fluxbox and xdm, and if I open a term and do init 3 it'll bring up xdm. Does the same with gdm. I believe there's a "correct" command or something to do this, but what it is I don't know. Just an idea.

Link to comment
Share on other sites

cardassianscot, what do you mean with login out? login-out from Gnome? If a user logs in 'only' through gdm (kdm...), then at login-out all it's process are killed.

 

Could you explain a little bit more how does your script work.

 

Edited:

Side note: why don't you make a script that starts while the user prints and "stops" just after the printing is done? (IMHO that would be simplier); for example:

 

move your print command (lets say that is /usr/bin/lpr) to /usr/bin/lpr-bin and create a bash (or perl) script like this one:

[root@localhost ~]$ mv /usr/bin/lpr usr/bin/lpr-bin

[root@localhost ~]$ touch /usr/bin/lpr && chmod +x /usr/bin/lpr

then edit the script:

[root@localhost ~]$ cat > /usr/bin/lpr

#! /bin/bash



ARGS="$@" # command line args

# or (maybe better):

# ARGS=" -Pprinter-name@printer-server $@"



USER="$USER" # or USER=$(whoami)



init_message="Hi, $USER printer printing"

end_message='printer ended its job. Bye!'



if [[ "$DISPLAY" ]]; then # behave different in X than in text-mode.

    ECHO() { echo "$@" | /usr/X11R6/bin/xmessage -file - & } # pops up a window 

else

    ECHO() { echo "$@"; } # simple echo

fi



ECHO "$init_message"

/usr/bin/lpr-bin "$ARGS"  && ECHO "$end_message"



exit 0

 

That would be much simplier, wouldn't be?

 

(Notice that the code above isn't tested, is just an idea to show what I mean. Keep in mind that is wrote on the fly, so it probably wont work)

Link to comment
Share on other sites

I do mean logining in through kdm and then loggin our through kdm. I know most processes are killed when you logout but for some reason this one isn't. (checked through flip to a text terminal and ps -A). This might have something to do with the type of script it is. It is essentially a network server which listens on a port and when it recieves data it forks a x-window to display a message.

 

Having a killall script is a good idea but where do I put it or which script do I need to call it from to run on logout?

 

Having a script that only runs when someone prints something might solve this one but complicates matters since

1) I have no idea how to start a script when someone prints something

2) the print server is not running on the local machine

 

My script works by users printing to a file (the actual file is irrelevant but it means nothing prints) and then a script looks at the job files and finds out who printed, from what computer and how many pages. If the user has enough credits then it prints it to the real printer otherwise it blocks it. In anycase it sends a message to a program on the machine the file was printed from, which just pops up a window with the message in it. It's this program running on the local machine which I need to start and stop when a user logs in or out.

 

Thanks

Link to comment
Share on other sites

I do mean logining in through kdm and then loggin our through kdm. I know most processes are killed when you logout but for some reason this one isn't. (checked through flip to a text terminal and ps -A). This might have something to do with the type of script it is. It is essentially a network server which listens on a port and when it recieves data it forks a x-window to display a message.

probably not with the type of script, but by the way, and which program, is launching it.

(sorry, I can't help with kdm, I don't use it due its particular behavior in many things)

Having a script that only runs when someone prints something might solve this one but complicates matters since 

1) I have no idea how to start a script when someone prints something

As I told you, replacing the printer command with a 'launcher' script
2) the print server is not running on the local machine
that's why I suggest the -P option in the lpr command.
My script works by users printing to a file (the actual file is irrelevant but it means nothing prints) and then a script looks at the job files and finds out who printed, from what computer and how many pages. If the user has enough credits then it prints it to the real printer otherwise it blocks it. In anycase it sends a message to a program on the machine the file was printed from, which just pops up a window with the message in it. It's this program running on the local machine which I need to start and stop when a user logs in or out. 

 

Thanks

I see, then you'll need a KDE (kdm) equivalent to user's ~/.bash_logout or ~/.logout or find a way that launching the script ensures the kill when kde exits

Link to comment
Share on other sites

I need to start and stop when a user logs in or out.

I have searched a bit in my system, and this is what I've find out:

  • you'll find:

/etc/X11/gdm/PreSession/

/etc/X11/gdm/PostSession/

to place scripts to run before and after the user's session. I think PostSession will be a good place to have a killall script.

 

[*] on xdm you have:

/etc/X11/xdm/GiveConsole

/etc/X11/xdm/TakeConsole

which are scripts with the same function as Pre/PostSession had in gdm; so TakeConsole will be the place to call the killall script.

 

[*] on kdm which is what you are interested in, I have to say sorry, but I have no idea :cry: , I don't use neither KDE, nor kdm, but I really hope that those gdm's and xdm's clues will put you in the right path to solve your problem.

good luck

Link to comment
Share on other sites

Thanks Aru, I think I get what you were saying now. Sorry for not picking it up properly first time. It will need a bit more coding to get it working (I need to make sure that if a new print job is started before the first one has printed then both messages get through [this isn't a plea for help here, just outlining why this approach did not immediately strike me as easier]) but thank you for pointing me in the right direction.

 

Or I may just change over to GDM, I haven't yet found the file or directory for running commands when logging out in KDM.

 

I agree that it would be better to put my scripts into the print server and I have every intention of doing so, however I am waiting for CUPS 1.2 to do this since it has a facility for adding scripts to handle quotas. However, until then my users still deserve a working system and as I said my users just seem to ignore the email way.

 

Once again thanks.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...