Find the answer to your Linux question:
Results 1 to 8 of 8
I am trying to run a simple script to let me know if rtorrent is running under a user. Code: #!/bin/sh SERVICE='rtorrent' USER='0001' if pgrep -u $USER $SERVICE > /dev/null ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    6

    Simple Service Check Script


    I am trying to run a simple script to let me know if rtorrent is running under a user.

    Code:
    #!/bin/sh
    
    SERVICE='rtorrent'
    USER='0001'
    
    if pgrep -u $USER $SERVICE > /dev/null
    then
        echo "$SERVICE service is active"
    else
        echo "$SERVICE is not active"
    fi
    The script always returns rtorrent is not active even though it clearly is.

    Any help is welcome.

  2. #2
    Linux User
    Join Date
    Dec 2009
    Posts
    260
    You may wannt change it into something like that:

    Code:
    #!/bin/sh
    
    SERVICE='rtorrent'
    USER='0001'
    
    if [ -z $(pgrep -u $USER $SERVICE) ]
    then
        echo "$SERVICE service is active"
    else
        echo "$SERVICE is not active"
    fi
    Or (Ain't sure of that one)
    But if I remember right $? is the exit code of the last command.
    I don't know if "pgrep" will exit with an error if no process is found.

    Code:
    #!/bin/sh
    
    SERVICE='rtorrent'
    USER='0001'
    
    pgrep -u $USER $SERVICE > /dev/null
    if $?
    then
        echo "$SERVICE service is active"
    else
        echo "$SERVICE is not active"
    fi

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    13
    Hi,
    You can try this code.

    #!/bin/bash

    SERVICE='rtorrent'
    USER='0001'

    srvchk=`pgrep -u $USER $SERVICE`
    if [ $? -eq 0 ]
    then
    echo "$SERVICE service is active"
    else
    echo "$SERVICE is not active"
    fi

  4. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Or you check if there's the .pid file created by rtorrent at startup, if so in that pid file should be the process id of rtorrent. Then you can check if a process with that id exists. Only then the rtorrent service is running.

    Note: on a power loss rtorrent does not remove the .pid file and this leaves the pid file with a invalid process id that should be cleaned by the startup script.

    Cheers

  5. #5
    Administrator jayd512's Avatar
    Join Date
    Feb 2008
    Location
    Kentucky
    Posts
    5,023
    Does it need to be a script?
    I may be over-simplifying this, but how about just using top?
    Code:
    top | grep rtorrent
    Jay

    New users, read this first.
    New Member FAQ
    Registered Linux User #463940
    I do not respond to private messages asking for Linux help. Please keep it on the public boards.

  6. #6
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    This is normally not enough. Thinking of the setup I have at home there's one instance of rtorrent that is started and run as a service with a dedicated session directory. Still each user could possibly start his own rtorrent instance on another free port and a different session directory. And even if this was not possible, I would not do:

    Code:
    top | grep rtorrent
    but

    Code:
    ps -A | grep rtorrent
    cheers

  7. #7
    Administrator jayd512's Avatar
    Join Date
    Feb 2008
    Location
    Kentucky
    Posts
    5,023
    Gotcha. And I see what you mean.
    Guess I was thinking to simplistic.
    Jay

    New users, read this first.
    New Member FAQ
    Registered Linux User #463940
    I do not respond to private messages asking for Linux help. Please keep it on the public boards.

  8. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    And to OP, if you are not worried just about the 'rtorrent' client, then I'd be a bit less specific about what I'm looking for.

    For example, something like this would look for any instance that has 'torrent' somewhere in it, and return the name of the user responsible:

    Code:
    ps -eo user,cmd|awk '/torrent/{print $1}'
    Also, keep an eye on open TCP ports:
    Code:
    netstat -tna
    or with lsof:
    Code:
    lsof -i

Posting Permissions

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