How to get the pid of the last executed command in shell script? my-app & export APP_PID=$!

ubu1404@ubuntu1404lb01:~/temp$ sleep 10 &
[1] 3622
ubu1404@ubuntu1404lb01:~/temp$ echo $!
3622
ubu1404@ubuntu1404lb01:~/temp$

Get PID:
#!/bin/bash
my-app & echo $!
Save PID in variable:
#!/bin/bash
my-app & export APP_PID=$!
Save all instances PID in text file:
#!/bin/bash
my-app & echo $! >>/tmp/my-app.pid
Save output, errors and PID in separated files:
#!/bin/bash
my-app >/tmp/my-app.log 2>/tmp/my-app.error.log & echo $! >>/tmp/my-app.pid

echo "my-app PID's: $(cat /tmp/my-app.pid)"

Comments

Popular posts from this blog

HAproxy logging

tomcat catalina coyote jasper cluster

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all