In a bash script I am starting openvpn as a client like this:
#!/bin/bash
conf_file=/etc/openvpn/blahblah.conf
openvpn_pid_file="/var/run/openvpn-client/ovpnc.pid"
command_line="/usr/bin/openvpn --config $conf_file --daemon"
$command_line
openvpn_pid=$!
echo "openvpn_pid=$openvpn_pid"
echo $openvpn_pid >> "$openvpn_pid_file"
After a successful start of openvpn, my variable openvpn_pid is empty and my openvpn_pid_file is empty. However, pgrep openvpn
will give me the PID. Why am I not getting that PID in my script? What should I change in order to get the correct PID in my variable and ultimately into the openvpn_pid_file?
This is all on Arch Linux.
From
man bash
:In other words:
$!
only contains a value if the process is backgrounded. Because you did not background a process from the shell,$!
is empty, and therefore,openvpn_pid
is empty.As for the solution, it's in Sven's comment.
As for