0

I have a script file using start-stop-daemon:

trap "" 1
(
    echo "INFO Starting $BinName application"
    cd ${APPLICATION_ROOT}
    if [ -e ${DEVOUT} ]; then
        mv ${DEVOUT} ${DEVOUTOLD}
    fi
    if [ -e ${DEVERR} ]; then
        mv ${DEVERR} ${DEVERROLD}
    fi
    if [ -e ${VALGRIND_LOG} ]; then
        mv ${VALGRIND_LOG} ${VALGRIND_LOG_OLD}
    fi
    # rm ${VALGRIND_LOG}
    HASHCODE=$(sha1sum ${APPLICATION_BIN}/${BinName}|cut -b0-40)
    if [ ! -z "$VALGRIND_ENABLE" ]; then
        VALGRIND_CMD="$VALGRIND --log-file=$VALGRIND_LOG"
    fi
    start-stop-daemon -S -b -m -p ${PidFileName} -c service:c10 --startas /bin/bash -- -c "/usr/bin/nohup $VALGRIND_CMD ${APPLICATION_BIN}/${BinName} -j 8019 -O -H $HASHCODE 2>${DEVERR}  > ${DEVOUT}  &"
)
sleep 1
pid=$(mypidof $BinName)
echo $pid > $PidFileName
if [ X$pid !=  "X" ]; then
    RETVAL=0;
    echo "INFO $BinName running with pid=${pid}"
else
    RETVAL=11;
    echo "ERROR Start $BinName failed !!!"
fi  

I want to execute the script from C++ code. I tried to use system()/popen()/execv() but start-stop-daemon doesn't run (process it called is not running). I don't know if I have missed anything to do.

If I run the script file directly, start-stop-daemon works properly.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
pinxpoong
  • 11
  • 1
  • How are you running the script directly? Since it does not have a `#!` line, the system cannot just execute it directly. You could do `system("bash script.sh start")` to run it, or you could add `#! /bin/bash` as the first line and run it as an application. Also remember `start-stop-daemon` must be run as root. – Tim Roberts May 19 '23 at 04:49
  • @TimRoberts Sorry, This is just a snippet of the script file and I guarantee it works fine when used directly. And I also think about permission, I will try to set it to root. – pinxpoong May 19 '23 at 07:29
  • This problem solved with root privileges. – pinxpoong May 19 '23 at 09:21

0 Answers0