:)Hi there, I am new to scripting and wanted to see if someone can show me how to grep on multiple processes and send the output to a file in /home/mydir/output .
I am aware of
ps -ef | grep on 1 process
but need help looking up multiple processes, can you use this command
You don't need grep at all. There's no point doing so if you already have the PID's, and if you don't have the PID's, there's probably better ways to get them than grepping ps.
The ps command syntax varies across versions of unix and Linux but I have never seen the syntax posted by @Corona688. The PID value is always prefixed by a p or -p in my experience.
Please post what Operating System and version you are running and what Shell you use.
The easy solution is run ps more than once. No point in running ps -e unless you need to see all the processes. Avoid obvious variable names like $PID and $PPID because they are known to interfere with the operation of some versions of ps .
# Assuming /home/mydir/output is a directory (not clear in original post)
mylogfile="/home/mydir/output/mylogfile"
touch "${mylogfile}"
# Invented pid's 1000, 2000, 3000
for pid_item in 1000 2000 3000
do
# Not sure about the -l parameter, so left it out for the moment
# Might need a "grep -v" to get rid of the headings. Depends on expected output (not posted).
ps -fp ${pid_item} >> "${mylogfile}"
done
Some versions of ps will accpt syntax like:
ps -fp "1000,2000,3000"
Depends what ps you have and what Operating System and version.
Ps. Syntax like ps -ef|egrep "1000|2000|3000" is prone to false hits and can overload the kernel and give you false negatives (seen many times ... before someone posts a contra).