Find the answer to your Linux question:
Results 1 to 9 of 9
Dear reader, Im pretty new with Linux and this forum, so if I post this on the wrong section; please correct me. atm I use a centos VPS with 1gb ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    8

    Screen start/stop script with a name?


    Dear reader,

    Im pretty new with Linux and this forum, so if I post this on the wrong section; please correct me.
    atm I use a centos VPS with 1gb memory and enough space and brandwidth (if this info is off any use).

    What I want to achieve is that I can start and quit a process that is running in a screen, with using a script.

    Like start.sh that does "screen serverapp1"
    and stop.sh that does "kill serverapp1"

    I already tried something like that, but it didnt work. The kill command only works with process ID's and that one is variable.

    Kinds,

    Guido
    Last edited by zondvloed; 09-24-2010 at 11:24 AM.

  2. #2
    Linux Engineer Segfault's Avatar
    Join Date
    Jun 2008
    Location
    Acadiana
    Posts
    877
    Try with pkill or killall.

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    8
    Quote Originally Posted by Segfault View Post
    Try with pkill or killall.
    [servercod4001@datacenter-196 ~]$ screen -ls
    There is a screen on:
    11343.servercod4001 (Detached)
    1 Socket in /var/run/screen/S-servercod4001.

    [servercod4001@datacenter-196 ~]$ killall -u servercod4001
    /proc is empty (not mounted ?)
    [servercod4001@datacenter-196 ~]$ killall servercod4001
    /proc is empty (not mounted ?)

  4. #4
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    Welcome to the forums zondvloed,

    It seems your VPS isn't a full environment, or you don't have the right permissions. I could be wrong, but I think tools relying on /proc to be mounted wont work, alas.

    That said, screen stores it's pids like this:
    Quote Originally Posted by zondvloed
    [servercod4001@datacenter-196 ~]$ screen -ls
    There is a screen on:
    11343.servercod4001 (Detached)
    1 Socket in /var/run/screen/S-servercod4001.
    So getting the PID is just a matter of prying it out of the named pipe:
    Code:
    kill `screen -ls | grep "tached" | cut -d'.' -f1`
    Note: "tached" will match both Attached and Detached. It'll also error when there are multiple sessions. You could do a
    Code:
    for i in `screen -ls | grep "tached" | cut -d'.' -f1` ; do kill $i ; done

    ___
    That is, assuming `pidof` wont work. Otherwise:
    Code:
    kill `pidof processname`
    Would suffice. Worth a try.
    Can't tell an OS by it's GUI

  5. #5
    Just Joined!
    Join Date
    Sep 2010
    Posts
    8
    thank you very much The first one works, so my problem is solved.

    But now for the educational part;
    what does "tached" do? what is it?

    and what does pidof do?

  6. #6
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    Quote Originally Posted by zondvloed
    But now for the educational part;
    what does "tached" do? what is it?
    Nothing. It's a passive and fully arbitrary pattern, but I don't think I'm much clearer now

    I believe 'grep' was called thus to abbreviate 'global/regular expression/pattern' or something similar. Basically, you send it a stream of data and let grep pick out the parts that you want. So it goes like this:

    Code:
    screen -ls
    gives the output:
    Code:
    There is a screen on:
    11343.servercod4001 (Detached)
    1 Socket in /var/run/screen/S-servercod4001.
    But we're only interested in line 2. So we pluck that out. There's thousands of ways to do that. The one I chose was to point grep at one of it's distinctive features. Well, I don't know the name of the session, and I don't know the pid, but I do know it's state can be either 'Attached' or 'Detached', and what binds these words is that they both have 'tached' in their name. Any recognizable and unique pattern in the string could have worked.

    Code:
    screen -ls | grep "tached"
    Gives the output:
    Code:
    11343.servercod4001 (Detached)
    And I only need the first number, so using the period '.' as a delimiter:
    Code:
    screen -ls | grep "tached" | cut -d'.' -f1
    Gives the output:
    Code:
    11343
    Now I feed that to kill by putting it in `` marks (not to be confused with the single quote ' )
    Code:
    kill `screen -ls | grep "tached" | cut -d'.' -f1`
    is expanded by the shell and interpreted:
    Code:
    kill 11343

    Quote Originally Posted by zondvloed
    and what does pidof do?
    pidof does what it's name implies. It gets you the 'pid of' a process.

    You would be helped if the man pages are installed on your VPS
    Try `man commandname` such as, indeed, `man pidof` or `man grep`. Other wise things like `grep --help` should give a short version of the options.

    And more about using | ; ` & " $ \ > and other goodies in the Advanced Bash-Scripting Guide
    Can't tell an OS by it's GUI

  7. #7
    Just Joined!
    Join Date
    Sep 2010
    Posts
    8
    thank you very much for your kindness and help

    Yes, but most off the time I dont understand the manual without an example though it was lazy off me not to google it; sorry.
    pidof only works for the root, could I implement it for my selfmade user?

  8. #8
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    Quote Originally Posted by zondvloed
    pidof only works for the root, could I implement it for my selfmade user?
    It appears your regular user doesn't have /sbin in it's path. I hadn't thought of it, because I always put /sbin /usr/sbin and /usr/local/sbin in my users path... that way your 'command not found' gets upgraded to a much more decisive sounding 'permission denied' when you're doing things you're not supposed to. But pidof will work...

    Anyway, when you're scripting it's always good to specify the full path name:
    Code:
    /sbin/pidof

    Quote Originally Posted by zondvloed
    thank you very much for your kindness and help

    Yes, but most off the time I dont understand the manual without an example though it was lazy off me not to google it; sorry.
    Then I'll leave you this as an excercize
    Can't tell an OS by it's GUI

  9. #9
    Just Joined!
    Join Date
    Sep 2010
    Posts
    8
    ah jeah, the /proc dir was still on 550, I putted it on 555; since Im the only person who gets there and reading and executing is not too bad to allow there ^^

    now /sbin/pidof; works

    thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •