2

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

8
  • 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, 2021 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) Jun 1, 2021 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.
    – bac0n
    Jun 1, 2021 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 Jun 1, 2021 at 16:45
  • 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.
    – user10489
    Jun 3, 2021 at 12:44

3 Answers 3

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:

2
  • is not what i need i ask how to change the process name (no executable, no symbolic link to file no executing symlink). Thank you Jun 1, 2021 at 16:26
  • You can't change the name in bash 4 except by a method like this. Bash 5 or zsh can. Hm, maybe need an answer with a zsh script.
    – user10489
    Jun 4, 2021 at 5:19
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
3
  • My wait works perfectly. I don't need a_script.sh and b_script.sh. You answered to an other question. but thank you Jun 1, 2021 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?)
    – bac0n
    Jun 1, 2021 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 Jun 1, 2021 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.