How to Build a Robust Init Script for a Swoole Mail Server on Linux
This guide explains how to create a full-featured init script for a Swoole‑based mail service, enabling start, stop, restart, reload, and status commands, plus automatic boot‑time activation, with complete shell code and step‑by‑step instructions.
Many services like nginx, MySQL, and php‑fpm provide init scripts for start/stop/restart, but Swoole services often lack such management and are manually killed. This article shares a method to create a comprehensive init script for a Swoole‑based mail server (zmail_server), enabling start, stop, force‑quit, restart, reload, reloadtask, and status commands.
Goal
Implement basic start|stop|force-quit|restart|reload|reloadtask|status commands.
Idea
Set process names for master and manager processes using swoole_set_process_name, then retrieve their PIDs by searching the process list, allowing various control actions.
Key Points
Assume the service name is zmail_server (see https://github.com/shenzhe/zmail).
1. In the onStart callback, set the master process name.
swoole_set_process_name('zmail_server running tcp://0.0.0.0:9501 master:' . $server->master_pid);
2. In the onManagerStart callback, set the manager process name.
swoole_set_process_name('zmail_server manager:' . $server->manager_id);
Full Script (modify as needed)
#!/bin/sh
### BEGIN INIT INFO
# Provides: mail_server
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts mail_server
# Description: starts the PHP FastCGI Process Manager daemon
### END INIT INFO
PHP_BIN=/usr/local/php/bin/php
SERVER_PATH=/home/jing.wang/mail
getMasterPid() {
PID=`/bin/ps axu|grep zmail_server|grep master|awk '{print $2}'`
echo $PID
}
getManagerPid() {
MID=`/bin/ps axu|grep zmail_server|grep manager|awk '{print $2}'`
echo $MID
}
case "$1" in
start)
PID=`getMasterPid`
if [ -n "$PID" ]; then
echo -n "mail server is running"
exit 1
fi
echo -n "Starting mail server "
$PHP_BIN $SERVER_PATH/webroot/main.php default
echo " done"
;;
stop)
PID=`getMasterPid`
if [ -z "$PID" ]; then
echo -n "mail server is not running"
exit 1
fi
echo -n "Gracefully shutting down mail server "
kill $PID
echo " done"
;;
status)
PID=`getMasterPid`
if [ -n "$PID" ]; then
echo -n "mail server is running"
else
echo -n "mail server is not running"
fi
;;
force-quit)
$0 stop
;;
restart)
$0 stop
$0 start
;;
reload)
MID=`getManagerPid`
if [ -z "$MID" ]; then
echo -n "mail server is not running"
exit 1
fi
echo -n "Reload service mail_server "
kill -USR1 $MID
echo " done"
;;
reloadtask)
MID=`getManagerPid`
if [ -z "$MID" ]; then
echo -n "mail server is not running"
exit 1
fi
echo -n "Reload service mail_server"
kill -USR2 $MID
echo " done"
;;
*)
echo "Usage: $0 {start|stop|force-quit|restart|reload|reloadtask|status}"
exit 1
;;
esacEnable Autostart
Place the script at /etc/init.d/zmail_server.
Make it executable: chmod +x /etc/init.d/zmail_server.
Add to startup: chkconfig --add zmail_server.
Result
Run /etc/init.d/mail_server start and see the service start similarly to nginx/php-fpm.
Now the mail server can be managed like other Linux services.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
