linuxbashloopspidkill

Killing a while loop that's running in the background without a PID


I'm a beginner in everything that is part of Linux so please take me slow.

I've created a "script" that's running in the background:

while true; do echo "I'm alive" >> alive.log ; done &

The output of the script is saved in a file alive.log that's present in the user's home directory. The problem is I have no ideea how to kill the loop since it's filling my disk space, if I wish to delete the file then loop will create a new file and fill it with the text "I'm alive" as I've asked it to do.

I tried using:

ps - aux | grep while

or

ps - aux | grep alive 

The output for the two lines will give me the PID I need but the problem is that the script is a loop which means the PID will change every time it runs itself (recursive) so I can't use the PID to kill the process.

I also tried using:

pkill while
killall while

The result for both lines is 0 (output can be seen when using pkill while -c "0" or killall while : "while: no process found";

Any suggestions please?


Solution

  • I wrote the sentence in a script file which named while.sh, and ran it by shell:

    [edemon@CentOS workspace]$ ./while.sh 
    [edemon@CentOS workspace]$ 
    

    There is not PID. I used top command tool to search my while.sh, it told me:

      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                 
     4036 edemon    20   0  5268  756  436 R 97.3  0.0   0:07.93 bash                                                                    
     2469 root      20   0 94412  29m  10m S  7.8  2.0   1:49.19 Xorg                                                                    
     2788 edemon    20   0 74300  12m  10m S  1.9  0.9   1:38.79 nm-applet                                                               
     4040 edemon    20   0  2708 1072  796 R  1.9  0.1   0:00.01 top 
    

    The while's father process is bash, so I killed 4036. The size of alive.log didn't grow any more.