Find the answer to your Linux question:
Results 1 to 5 of 5
I am writing a tcsh script that connects to another machine and starts a process, then gives it additional commands. However, I am not entirely sure how to do this ...
  1. #1
    Just Joined!
    Join Date
    Jul 2011
    Posts
    3

    tcsh script - find running process with grep

    I am writing a tcsh script that connects to another machine and starts a process, then gives it additional commands. However, I am not entirely sure how to do this properly, as in locating the process after it has been started. Here is what I am thinking so far:

    Code:
    ssh "<machine name>" -l <user> <start process command>
    ssh "<machine name>" -l <user> ps -ax | grep <process name>
    ...
    But I am unsure of how to proceed from here - and I don't know an extensive amount of shell commands.
    I would appreciate any advice - am I even going in the right direction with this?

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    Possible, but this approach might become "spaghetti code" quite soon, because of potential use of multipe commands and escapes.
    Also, it is not the best approach to open a ssh connection for each and every command. This will make it quite slow.

    You could use parallel-ssh (called pssh on some distributions) and despite the name: useable even for one machine.
    pssh offers you to direct the standard out and standard error of each host to an indivial file
    and especially it has the --send-input flag.
    With that, you can write little macros and dont need to care about extra escaping.

    Like this
    Code:
    mkdir ~/pssh-workdir/{err,out,macros}
    cat >> ~/pssh-workdir/macros/test1<<'EOF'
    ls -la
    ps auwx | grep blah
    EOF
    
    cat >> ~/pssh-workdir/hostlist<<'EOF'
    <host1>
    <host2>
    EOF
    
    
    cat ~/pssh-workdir/macros/test1 | pssh --send-input -h ~/pssh-workdir/hostlist -o ~/pssh-workdir/out -e ~/pssh-workdir/err
    But tbh, I would approach that differently:
    - management/maintenance with Puppet
    - and monitoring with Xymon

    Want to ensure a daemon is running?
    Code:
    service { <daemon>: ensure => running }
    See this for details: Documentation | Puppet Labs - Module Organization
    I like that

    That keeps it running, until something bad happens (syntax error in daemon config, out of disk space, hardware error, etc
    So monitoring is needed. And my favourite for this is
    green : Xymon - Status @ Wed Jul 13 14:27:51 2011
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Jul 2011
    Posts
    3
    Thanks for the advice - unfortunately, due to the way the system this will be running on is set up, I can't store anything in files, create new directories, or use any external tools, like puppet or xymon.
    Spaghetti code is not really a concern here, as the entire script is fairly short, and is a single strand of spaghetti anyway.

    So I would appreciate it if you could please tell me what I would need to add to the code in my original post (and preferably explain how it works) in order to give the just started or long-running process a command, such as an order to do something or a kill signal.

  4. #4
    Just Joined!
    Join Date
    Jul 2011
    Posts
    3
    I still need help with this, please respond.

  5. #5
    Linux Guru
    Join Date
    May 2011
    Posts
    1,842
    Quote Originally Posted by Adorai View Post
    So I would appreciate it if you could please tell me what I would need to add to the code in my original post (and preferably explain how it works) in order to give the just started or long-running process a command, such as an order to do something or a kill signal.
    Well, an order to do something is kind of ambiguous. If the process you want to control supports accepting signals then, great, just send them - you have a remote shell in. If you want to kill it, just send the kill sig to the pid,.e.g
    Code:
    # get the pid
    pids=$(ssh USER@REMOTE "ps -eo pid,cmd|awk '/myprog/{print \$1}'")
    
    # kill the pid
    ssh USER@REMOTE kill -9 $pids

Posting Permissions

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