0

I'm a newbie in linux administration and I'm running a web server with cgi, and in the bellow script, I want to do SIGHUP to x PID, but from the html when I fullfill the PID number and I press on the "STOP BUTTON" that correspond to my first if condition, it doesn't kill the PID. Pasting the same line "sudo kill -1 PID" directly in the console, I can kill it.

> #!/bin/bash

LOG_FILE="/usr/lib/cgi-bin/user.log"
arr=(${QUERY_STRING//[=&]/ })
opcion=${arr[1]}
NombreProceso=${arr[3]}

Fecha=$(date)
echo "-Procesos-" >> user.log
echo "$opcion" >> user.log
echo "$NombreProceso" >> user.log
if [ $opcion = 1 ];
then
    sudo kill -1 $NombreProceso
    echo "$Fecha::Paramos el proceso $NombreProceso" >> user.log
    echo "$opcion" >> user.log
fi
if [ $opcion = 2 ];
then
    sudo kill -19 $NombreProceso
    echo "$Fecha::Pausamos el proceso $NombreProceso" >> user.log
    echo "$opcion" >> user.log
fi

echo Content-Type: text/html
echo -e "
<html>
    <head>
        <title> UOLS WEB Unai Ayoub Jaime</title>
    </head>
    <body>"
echo -e "<textarea style='width:100%;height:90%; resize: none' disabled>"
        ps -aux | less
        echo -e "</textarea>"
echo -e "
    </body>
</html>
"
  • Does it have to be by a particular PID? If you want to kill every instance of a process by name you can use `pkill` based on full or [partial](https://stackoverflow.com/questions/8987037/how-to-kill-all-processes-with-a-given-partial-name) patterns. You can also use `pkill` to selectively kill processes based on args; thus avoiding PIDs. – Andy J Apr 10 '20 at 16:14
  • Not a particular PID. It can be foo PID. I've tried it before but it didn't works, and also, in the apache error log, I get :sudo: no tty present and no askpass program specified: /usr/lib/cgi-bin/ps.sh, referer: http://192.168.0.24/index1.html – dahchour ayoub Apr 10 '20 at 16:53

1 Answers1

0

Finally I got it. The problem was in the file sudoers I excecute it as a root visudo and I put bellow root ALL... www-data ALL=NOPASSWD: /bin/kill so the apache2 user "www-data" can kill any pid for example, if I want to execute a reboot order, I should add to www-data ALL=NOPASSWD: /bin/kill,/sbin/reboot so I can handle with that command. And don't forget to give the script as a owner www-data.