running bash piped comand from mono linux - linux

I am trying to run bash command with pipe using mono process start
here is code snippet I am trying
single commands works, however pipe commands fails to run, what am I missing here ?
ProcessStartInfo oInfo = new ProcessStartInfo(command, args);
oInfo.UseShellExecute = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;
StreamReader srOutput = null;
StreamReader srError = null;
Process proc = System.Diagnostics.Process.Start(oInfo);
proc.WaitForExit();
I tried running "ps -aux" which runs fine. However ps -aux | grep gnome command failed.
I tried these scenarios
scenario 1:
command = ps
argument = -aux | grep gnome
scenario 2:
command = ps
argument = "-c ' -aux | grep gnome ' "
scenario 3 :
command = ps
argument = " -c \" -aux | grep gnome \" "
all these failed with
error: garbage option
Usage:
ps [options]
Try 'ps --help '
or 'ps --help '
for additional help text.
Also on side question, the reason I am trying to do this is to figure out of a particular daemon is already running. Is there a standard way to get this info.
for instance in windows we can query running services using ServiceController.GetServices().
Is something similar available on mono/Linux directly ?

When you add "|" to a bash line the bash interpreter splits the command in two processes and feeds the output from one to the other, when you call that command using Process it sends the argument as is.
The most close you can achieve is to start yourself the two process and feed one with the output of the other through the input/output streams.
About part 2 of your question, if the program runs as privileged then Process.GetProcesses will list all running processes on the system included daemons.

Related

How to kill a script that has been executed using source?

The problem is that I cannot find the process ID of a script that has been executed using source. I am able to do so when they are launched with bash using ps -ef.
If I run a script using bash, I can figure the process ID using ps -ef | grep "test1.sh" | grep -v "grep". However, if I run the script using source, I cannot search for it and hence cannot find the process ID.
I have read the difference between the bash and source commands from this link.
This is my testing procedure:
I have 2 terminals. In one of them, I am searching for process IDs using ps -ef. In the other one, I run a script which prints 'Hello' every one second (an infinite while loop with sleep of 1 second). With bash, PID is searchable, but with source, grep doesn't get any results.
I am working on an Ubuntu 18.04.2 LTS machine
If you do not want to terminate the sourcing bash and are satisfied with the script being stopped only after a command (such as sleep) finishes, you can kill -INT the bash process.

Bash script to list all processes in the foreground process group of a terminal

How can I write a bash script to print out the PIDs of all processes in the foreground process group of a given terminal (which is different from the one in which I run the script)? I know that the C function tcgetpgrp can do the job, but I am wondering if there exist any command line utilities that can do this more easily.
To find the pids of all processes in the foreground process group of pts/29, you can do (on linux):
ps ao stat=,pid=,tty= | awk '$1 ~ /\+/ && $3 ~ /pts\/29/{ print $2}'
ps is often different, and I am uncertain of the portability of that solution.
You can use pgrep's -t flag, which enables you to list process using a given tty.
For example :
# on a first ssh session, which gets pts/0 :
sleep 10
# on a second ssh session :
pgrep -t "pts/0"
1234 # the first session's bash process
5678 # the first session's sleep process

SSH inside perl script hangs

I am trying to execute the following command using perl script :
my $sum = `$sudo $ssh $server netstat -Aan | /bin/grep 1158 | /bin/egrep '#IP[0]' | wc -l`;
Where $sudo = full path to sudo command , same for $ssh , $server = the file server that I want to pull the data from , and #Ip[0] = ip address.
When I run the exact command from the shell (not as a script) I get the required result (which is an integer number). However , when I run the script it just gets hangs when it reaches to that point.
I cant see anything wrong with the syntax , I am using back ticks to save the returned output , could you please assist?

Run a script in the same shell(bash)

My problem is specific to the running of SPECCPU2006(a benchmark suite).
After I installed the benchmark, I can invoke a command called "specinvoke" in terminal to run a specific benchmark. I have another script, where part of the codes are like following:
cd (specific benchmark directory)
specinvoke &
pid=$!
My goal is to get the PID of the running task. However, by doing what is shown above, what I got is the PID for the "specinvoke" shell command and the real running task will have another PID.
However, by running specinvoke -n ,the real code running in the specinvoke shell will be output to the stdout. For example, for one benchmark,it's like this:
# specinvoke r6392
# Invoked as: specinvoke -n
# timer ticks over every 1000 ns
# Use another -n on the command line to see chdir commands and env dump
# Starting run for copy #0
../run_base_ref_gcc43-64bit.0000/milc_base.gcc43-64bit < su3imp.in > su3imp.out 2>> su3imp.err
Inside it it's running a binary.The code will be different from benchmark to benchmark(by invoking under different benchmark directory). And because "specinvoke" is installed and not just a script, I can not use "source specinvoke".
So is there any clue? Is there any way to directly invoke the shell command in the same shell(have same PID) or maybe I should dump the specinvoke -n and run the dumped materials?
You can still do something like:
cd (specific benchmark directory)
specinvoke &
pid=$(pgrep milc_base.gcc43-64bit)
If there are several invocation of the milc_base.gcc43-64bit binary, you can still use
pid=$(pgrep -n milc_base.gcc43-64bit)
Which according to the man page:
-n
Select only the newest (most recently started) of the matching
processes
when the process is a direct child of the subshell:
ps -o pid= -C=milc_base.gcc43-64bit --ppid $!
when not a direct child, you could get the info from pstree:
pstree -p $! | grep -o 'milc_base.gcc43-64bit(.*)'
output from above (PID is in brackets): milc_base.gcc43-64bit(9837)

Why does ps only return one line of output in my Perl script when I call it with Nagios?

I have this running:
if (open(PS_ELF, "/bin/ps -eLf|")) {
while (<PS_ELF>) {
if ($_ =~ m/some regex/) {
# do some stuff
}
}
}
If called locally, the loop runs just fine, once for every output line of ps -eLf
Now if the same script is called from Nagios via NRPE, PS_ELF does only contain one line (the first line output by ps).
This puzzles me; what could be the reason?
Maybe this is not limited to/caused by Nagios at all, I just included it for the sake of completeness.
I'm on SUSE Enterprise Linux 10 SP2 and perl v5.8.8.
Although this problem is very old, I experienced the exact same problem today.
So I thought I share what I found.
The problem is that processes created by the NRPE daemon (can) have a different environment than processes you execute directly in the shell as the NRPE daemon user.
I created the following bash script:
#!/bin/bash
echo `env | grep COLUMNS`
This gives me the environment variable COLUMN of the current process, which has the same environment as the parent process (the process forked by the NRPE daemon).
When I execute this script as the NRPE daemon user
$ /tmp/check_env.sh
COLUMNS=174
it gives me the value of my current shell window.
But when I execute this script via NRPE, I get:
nagios-server $ check_nrpe -H client -c check_env
COLUMNS=80
Which is why ps -eaf output is limited to 80 characters unless you use the ww parameter for unlimited width, which ignores the COLUMNS environment variable.
I changed 'ps -eLf' to 'ps -eLfww' (ww for unlimited output) and this fixed the problem even if I don't understand why there is a difference when called remotely.
It's probably more something to do with how NRPE plugins work than Perl itself.
Your plugin is working like explained here (return code + output) ?

Resources