I have a script loaded as a service in /etc/init.d/myfile
When I try to start the service I get the error
/etc/init.d/myservice: 21: /etc/init.d/myservice: Syntax error: "(" unexpected
The issue seems to be with the process substitution <( in the source command. I use it without any problem in other scripts to extract variables from my main config file but inside a case statement I don't know how to make it work.
myservice contains:
#!/bin/sh
#/etc/init.d/myservice
### BEGIN INIT INFO
# Provides: myservice
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: my service
# Description: Start the myservice service
### END INIT INFO
case "$1" in
start)
# start processes
# Import the following variables from config.conf: cfgfile, dir, bindir
source <(grep myservice /opt/mysoftware/config.conf | grep -oP '.*(?= #)')
if [ -f $cfgfile ]
then
echo "Starting myservice"
/usr/bin/screen -U -d -m $bindir/myscript.sh $cfgfile
else
echo "myservice could not start because the file $cfgfile is missing"
fi
;;
stop)
# kill processes
echo "Stopping myservice"
screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill
;;
restart)
# kill and restart processes
/etc/init.d/myservice stop
/etc/init.d/myservice start
;;
*)
echo "Usage: /etc/init.d/myservice {start|stop|restart}"
exit 1
;;
esac
exit 0
The file config.conf is a list of variable declarations with a short description and the name of the script using them. I use grep filters to source only the variables I need for a given script.
It looks like this:
var1=value # path to tmp folder myservice
var2=value # log file name myservice script1.sh script2.sh
var3=value # prefix for log file script1.sh script2.sh
Note: The service worked fine before I converted it to start using the config file instead of hardcoded values.
Thank you.
<(...), since it's a nonstandard extension. If you change the first line to#!/bin/bash, does it work?