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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

scripting help

  • 10-04-2013 11:24am
    #1
    Registered Users, Registered Users 2 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


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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
  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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, Registered Users 2 Posts: 36,533 ✭✭✭✭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
    >

    In Cavan there was a great fire / Judge McCarthy was sent to inquire / It would be a shame / If the nuns were to blame / So it had to be caused by a wire.



  • Advertisement
  • Registered Users, Registered Users 2 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.


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭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, Registered Users 2 Posts: 2,755 ✭✭✭niallb


    xargs ftw :-)


Advertisement