Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

scripting help

Options
  • 10-04-2013 12:24pm
    #1
    Registered Users Posts: 23,212 ✭✭✭✭


    We have a large number of users connecting to our Red Hat Server via Remote Desktop (long story, don't ask).

    What is happening is that they are not logging out of RDP properly, they are simply closing the RD software on their client PC, leaving the XRDP sessions still running. As you can imagine, over time, these build up and prevent further users from connecting.

    As such, I want to run a nightly cron job that will kill the xrdp processes. So far, I have the following which gives me the list of PIDS:

    ps -ef pid | awk '$1 == "xrdp" { print $2 }'


    However, given this list of pids, I am having trouble piping it into the kill command:

    ps -ef pid | awk '$1 == "xrdp" { print $2 }' | kill
    ps -ef pid | awk '$1 == "xrdp" { print $2 }' > kill
    ps -ef pid | awk '$1 == "xrdp" { print $2 }' > kill_list.txt
    kill < kill_list.txt

    My scripting skills are quite rusty at this stage, so can anyone give me some direction on how to pipe this list into the kill command?


Comments

  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    Could you not just do killall xrdp?


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    by the way, what you are looking for is:

    for i in `cat kill-list.txt`;do kill $i;done


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    pkill xrdp (as root) should also work (once you're not connected by rdp of course :)).

    edit: I'm not sure that for loop will work with newline delimiters. I think you need a while loop with a readline:

    while read PID; do
    kill $PID
    done < kill-list.txt


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    One last thing: The reason this isn't working:

    ps -ef pid | awk '$1 == "xrdp" { print $2 }' > kill_list.txt
    kill < kill_list.txt

    Is that you're trying to pass it in through stdin so they're not command line arguments (as kill is expecting). You would probably get away with something like this:

    kill $(ps -ef pid | awk '$1 == "xrdp" { print $2 }')


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    Khannie wrote: »
    pkill xrdp (as root) should also work (once you're not connected by rdp of course :)).

    edit: I'm not sure that for loop will work with newline delimiters. I think you need a while loop with a readline:

    Why would it not work?


  • Advertisement
  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    I remember having issues with for loops and newlines before which made me move to the while loop (with read). The for loop is my favourite instrument of looping in bash by quite a margin, so for me to move to while for newline-y stuff there must have been some reason. I am too lazy to test it though. :D


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    Khannie wrote: »
    I remember having issues with for loops and newlines before which made me move to the while loop (with read). The for loop is my favourite instrument of looping in bash by quite a margin, so for me to move to while for newline-y stuff there must have been some reason. I am too lazy to test it though. :D

    Cat blah

    ghgh
    ghghg
    gjgjg
    djdjd
    eyrytyp

    for i in `cat blah`; do echo $i; done

    Result:
    ghgh
    ghghg
    gjgjg
    djdjd
    eyrytyp

    Not alot of testing involved. Hence my question.


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    Hmmm. Yeah I just tested it with kill and 2 pid's in a file separated by a newline and it worked perfectly. There was definitely some reason in the past that I used the while loop though (i.e. a for loop had failed). I wasn't familiar with the while read <VAR> syntax, so it required a googling and that's why I remember it. At some point many moons from now when it happens to me again I will come back here and post why it was.


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    xargs is good for this!
    # ps -ef | grep <process string> | awk {' print $2'} | xargs kill
    

    e.g.:
    # ps -ef | grep jdk | awk {' print $2'} | xargs kill
    

    or if you wanna be aggressive:
    # ps -ef | grep jdk | awk {' print $2'} | xargs kill -9
    


  • Registered Users Posts: 34,258 ✭✭✭✭Hotblack Desiato


    syklops wrote: »
    Cat blah

    ghgh
    ghghg
    gjgjg
    djdjd
    eyrytyp

    for i in `cat blah`; do echo $i; done

    Result:
    ghgh
    ghghg
    gjgjg
    djdjd
    eyrytyp

    Not alot of testing involved. Hence my question.

    It'll break if there are spaces in the lines. Not that your PIDs should have spaces :)

    > cat blah
    aaaaa
    bbbbb
    cc cc
    ddd d
    eeee e
    > for i in `cat blah`; do echo $i; done
    aaaaa
    bbbbb
    cc
    cc
    ddd
    d
    eeee
    e
    > while read PID;do echo $PID; done <blah
    aaaaa
    bbbbb
    cc cc
    ddd d
    eeee e
    >

    Fingal County Council are certainly not competent to be making decisions about the most important piece of infrastructure on the island. They need to stick to badly designed cycle lanes and deciding on whether Mrs Murphy can have her kitchen extension.



  • Advertisement
  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    syklops wrote: »
    Could you not just do killall xrdp?

    Wasn't aware of that command, that's exactly what I need. Thanks.

    I Just need to be careful not to kill the daemon too.


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    ninja900 wrote: »
    It'll break if there are spaces in the lines.


    That's the one. Thank you.


  • Closed Accounts Posts: 18,966 ✭✭✭✭syklops


    Tom Dunne wrote: »
    Wasn't aware of that command, that's exactly what I need. Thanks.

    I Just need to be careful not to kill the daemon too.

    Presumably(hopefully) the daemon is called xrdpd or something to distinguish it from the clients.
    It'll break if there are spaces in the lines. Not that your PIDs should have spaces

    Mystery solved, thanks for the input.


  • Registered Users Posts: 2,722 ✭✭✭niallb


    xargs ftw :-)


Advertisement