3

On my Ubuntu linux computer I'm making a loop like this using my shell bash

HERE MY CODE "./upperscript"

cat filename.txt | while read LINE || [[ -n $LINE ]];
do
    ./script $LINE >> $LINE.txt 2>> $LINE.err & sleep 1
    wait;
done > /dev/null 2>&1 &
echo "[PID $!]"

Here I go

Now, when the script disappears there are a lot of forked process (one at a time) over -bash name like this

username    15029  0.0  0.3   7404  3060 ?        S    mag31   0:00 -bash
username    13849  0.8  0.6  33048  6432 ?        S    mag31   0:08  \_ ./script HERE MY LINE
username    13869  0.1  0.3  33048  6451 ?        S    mag31   0:09    \_ ./otherscript HERE MY LINE
username    13881  0.1  0.1  33048  6465 ?        S    mag31   0:08    \_ ./otherscript HERE MY LINE

how to rename the process with pid 15029 that now it's called simply "-bash"?

I tryed also

$ chmod 777 /proc/15029/comm
chmod: changing permissions of '/proc/15029/comm': Operation not permitted

to do then

echo 'PROCESSNAME' > /proc/15029/comm"

but like u see the error is

"Operation not permitted" also using sudoers username or root (su)

if there's not a way, could I rename it from the process itself?

Saying using

bash -c "exec -a <MyProcessName> <Command>"

in a multiline command like my while?

Thanks a lot

  • 3
    Could you please explain what you are trying to achieve with your script ? Why you think you have to rename a running process ? And why you think that it is okay that the processes stil exists after the script as finished ? – Soren A May 31 '21 at 23:41
  • @Soren A "Could you please explain what you are trying to achieve with your script ? Why you think you have to rename a running process ? And why you think that it is okay that the processes stil exists after the script as finished ?" There are scripts that are working for administrating a little server. I want to rename it to understand faster what they are. When the scripts below terminate the script above also terminates (if u look at the code it's understandable) – Santoo Death XXXXXXXXXXXXXX Jun 01 '21 at 13:24
  • You use an OR statement that conditions an empty read to a non-empty test expression, which makes it redundant because it can never be true. –  Jun 01 '21 at 16:32
  • @bacon "You use an OR statement that conditions an empty read to a non-empty test expression, which makes it redundant because it can never be true." WTF r u saying? It works and well. I want only to change the name of the script. The script works and it nothing has to change in that. Thank you – Santoo Death XXXXXXXXXXXXXX Jun 01 '21 at 16:45
  • This will collect zombies without a wait, but I don't think that's the question. I think https://stackoverflow.com/questions/3251550/how-to-change-argv0-in-bash-so-command-shows-up-with-different-name-in-ps answers this question. – user10489 Jun 03 '21 at 12:44
  • @user10489 "This will collect zombies without a wait, but I don't think that's the question. I think stackoverflow.com/questions/3251550/…" answers this question." Yes! Me not, but if i don't kill with the $PGID it' full of zombie! :) P.S. Thanks. This question is a part of the response – Santoo Death XXXXXXXXXXXXXX Jun 03 '21 at 22:57
  • I think my statement was ambiguous. I meant that if you don't use wait, the process will eventually have a large collection of uncollected zombies until it dies itself. :) – user10489 Jun 03 '21 at 23:14
  • @user10489 You think good. Your statement don't was ambiguous. Wait there is at the end. I'm not a zombie! :) But already said that if I eventually don't kill it using $PGID there's a large collection of uncollected zombies. Here none dies and everything dies. But don't go off topic. Thanks 4 ur answer – Santoo Death XXXXXXXXXXXXXX Jun 04 '21 at 00:02

3 Answers3

1

I have the same issue. We can't do this

echo 'PROCESSNAME' > /proc/15029/comm"

It's a question of priviledges

0

Here is an easy way how to achieve what you want:

  1. Put your code in an executable file.
  2. For each fork create a symbolic link to that file.
  3. Execute the symlink instead of the original file.

Reference:

pa4080
  • 29,831
0

A quick demonstation how to use wait:

a_script.sh:

#!/bin/bash
i=5
while ((i--)); do
    $PWD/b_script.sh >> output
    sleep 2
done &
p=$! # because we just have one background
     # process this isn't really necessary.
echo Lets wait for all scripts to end.
read -p \
"meanwhile pick a number, fast! " n && echo you picked: $n
echo back to waiting...
wait $p
echo All done\!

b_script.sh:

#!/bin/bash
echo b_script | ts
  • My wait works perfectly. I don't need a_script.sh and b_script.sh. You answered to an other question. but thank you – Santoo Death XXXXXXXXXXXXXX Jun 01 '21 at 16:24
  • yes, but it's not needed, your while loop is already in the background, then also putting the script in the background and wait for it doesn't make sense. (maybe you have some more code in between?) –  Jun 01 '21 at 16:32
  • no I'dont have any other code in between. If for you had not sense could be that has not sense, but I don't run the script ./upperscript in background. it's in foreground and disappears leaving in background "-bash" that launch ./script $LINE. If you understand the code answer, if you don't understand the code there's a useless conversation. Thank you – Santoo Death XXXXXXXXXXXXXX Jun 01 '21 at 16:42