简体   繁体   中英

cat: pid.txt: No such file or directory

I have a problem with cat. I want to write script doing the same thing as ps -e. In pid.txt i have PID of running processes.

ls /proc/ | grep -o "[0-9]" | sort -h > pid.txt

Then i want use $line like a part of path to cmdline for evry PID.

cat pid.txt | while read line; do cat /proc/$line/cmdline; done

i try for loop too

for id in 'ls /proc/ | grep -o "[0-9]\+" | sort -h'; do 
cat /proc/$id/cmdline;
done

Don't know what i'm doing wrong. Thanks in advance.

You seem to be in a different current directory when running cat pid.txt... command compared to when you ran your ls... command. Run both your commands on the same terminal window, or use absolute path, like /path/to/pid.txt

Other than your error, you might wanna remove -o from your grep command as it gives you 1 digit for a matching pid. For example, you get 2 when pid is 423. @Roadowl also pointed that already.

I think what you're after is this - there were a few flaws with all of your approaches (or did you really just want to look at process with a single-digit PID?):

for pid in $(ls /proc/ | grep -E '^[0-9]+$'|sort -h); do cat /proc/${pid}/cmdline; tr '\x00' '\n'; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM