Mark Delphi
Mark Delphi

Reputation: 1765

How to kill hidden process?

I have the following script.

#!/bin/bash

if [ "$EUID" -ne 0 ]
  then
    echo '' 
    echo -e "\e[1;31m   Please run the script as root \e[0m"
    echo ''
  exit
fi

for run in {1..11}
do  
    echo -e '\e[1;32m   Initializing AP in backfround... \e[0m' 
    sudo screen -dmS hotspot
    sleep 5

    # start the AP in background
    echo -e '\e[1;32m   Starting AP in backfround... \e[0m'
    sudo screen -S hotspot -X exec ./start_hostapd.sh
    sleep 20

    # save PIDs for dmS
    ps -ef | grep "dmS" | awk '{print $2}' > dms.log
    sleep 1

    # save PIDs for hostapd
    ps -ef | grep "hostapd" | awk '{print $2}' > process.log
    sleep 1 

    echo -e '\e[1;33m   Running data... \e[0m'

    for run in {1..10}
    do  # send 10 times
        sudo /home/ubuntu/Desktop/send_data/run_data
        sleep 1
    done

    echo -e "\e[1;31m   Stopping sending... \e[0m"
    sleep 2

    echo -e "\e[1;31m   Quiting hotspot... \e[0m"
    sudo /home/ubuntu/Desktop/kill_dms/kill_dms
    sleep 5

    echo -e "\e[1;31m   Stopping AP... \e[0m"
    sudo /home/ubuntu/Desktop/kill_hostapd/kill_hostapd
    sleep 5

    echo -e '\e[1;31m   Wiping dead screens... \e[0m'
    echo
    sudo screen -wipe
    sudo screen -X -S hotspot quit
    sleep 5
done

I use a bash script that starts the AP (hostapd) and then it executes some another commands. Unfortunately, once the AP is started, the next lines will not be executed anymore. To avoid this problem, in the Script I start the AP using screen command that allows to run AP in background and also it allows to execute next lines.

For each iteration in the for-loop, the AP must be restarted. For this purpose I write out the PIDs of screen and hostapd and then I call my C programs, which kill these processes. At last I use screen commands again to ensure that the AP in the background has been stopped and it can be started again.

This implementation works good. However, when the script comes to the end and all processes has been already killed, the AP disappears in other devices and after some minutes it appears again and it happens several times. Only the system reboot helps to stop the AP completely.

I use htop to find out the processes which runs AP. However, I can not find the processes. The htop says that there is no processes, which I created using script from above. This is right, because the script kills the processes once it is finished.

So, I suppose that there are hidden processes for my AP and I do not see them. Is there a way to find that hidden processes and kill them to stop the AP?

When I just start the AP in another terminal and then I stop it just using CTRL+C, the AP will be stopped and my devices do not see it anymore.

That's why I suppose that the screen starts a hidden process, which can not be found by htop or by other programs like htop.

Upvotes: 1

Views: 2465

Answers (1)

XiR_
XiR_

Reputation: 222

If you don't need any hostap process at all, I'd rather use pkill instead of trusting the management of pids. Easiest usage should look like:

pkill -f hostap
pkill -f screen

If you'd want to use another signal like 9, use:

pkill -9 -f hostap
pkill -9 -f screen

https://linux.die.net/man/1/pkill

Upvotes: 1

Related Questions