linux - How do I get "awk" to work correctly within a "su -c" command? -
i'm running script @ end of jenkins build restart tomcat. tomcat's shutdown.sh script known not work in many instances , script supposed capture pid of tomcat process , attempt manually shut down. here command i'm using capture pid:
ps -ef | grep bootstrap | grep -v grep | awk '{print $2}' > tomcat.pid the output when manually runs retrieves pid perfectly. during jenkins build have switch users run command. i'm using "su user -c 'commands'" this:
su user -c "ps -ef | grep bootstrap | grep -v grep | awk '{print $2}' > tomcat.pid" whenever however, "awk" portion doesn't seem working. instead of retrieving pid, it's capturing entire process information. why this? how can fix command?
the issue $2 being processed original shell before being sent new user. since value of $2 in shell blank, awk command @ target shell becomes awk {print }. fix it, escape $2:
su user -c "pushd $tomcat_home;ps -ef | grep bootstrap | grep -v grep | awk '{print \$2}' > $tomcat_home/bin/tomcat.pid" note want $tomcat_home processed original shell it's value set properly.
Comments
Post a Comment