I have the following script
hello.sh
--------
while :
do
echo "hello"
sleep 20
done
And when I ran the above script, I got 2627 as the PID of the script
root@localhost ~]# ./hello.sh &
[1] 2627
[root@localhost ~]#
Now when I'm running the ps command in another terminal, I'm not getting the script name in the output
[root@localhost ~]# ps -ef| grep 262[7]
root 2627 1582 0 19:19 pts/1 00:00:00 -bash
root 3427 2627 0 19:52 pts/1 00:00:00 sleep 20
[root@localhost ~]#
$$ is the PID of the main shell where I'm running the script. I know that inside the script I can put $$ to get the PID of the script, but that's not my intention here.
Once I ran the long running script in the background and suppose I have closed the terminal accidentally, then is there a way to grep the PID using the script name or any other means?
FYI
[root@localhost ~]# echo $$
2586
[root@localhost ~]# ps -ef| grep 262[7]
root 2627 1582 0 19:19 pts/1 00:00:00 -bash
root 3657 2627 0 20:02 pts/1 00:00:00 sleep 20
[root@localhost ~]# ps -ef| grep bas[h]
root 1517 1513 0 18:43 pts/18 00:00:00 -bash
root 1582 1578 0 18:45 pts/1 00:00:00 -bash
root 2586 2581 0 19:18 pts/54 00:00:00 -bash
root 2627 1582 0 19:19 pts/1 00:00:00 -bash
[root@localhost ~]#
2586 is the PID of the main shell and inside it, I have run the hello.sh script. It created a new shell and started running the commands inside it. All these basic I know. This is not a duplicate question. Please see the output to understand it.
$$
– Kiwy Oct 22 '18 at 14:28