Mystic systemd startup/shutdown scripts.
For more information visit: https://vswitchzero.com/mystic-systemd

***

The 'Mystic Internet Service' (MIS) binary can run as a daemon, 
but treating it as a proper system service for system startup and 
shutdown requires a bit of extra work. 

In addition to just having the services start/stop at boot, I wanted
them to be able to better handle some common problems I was seeing. This 
includes:

1. Situations where the mis.bsy semaphore file isn't cleaned up when the 
MIS daemon is stopped. I've had this happen numerous times when MIS is 
stopped both gracefully and forcefully. The daemon can't be started 
until the file is manually cleaned up in these situations. The script 
checks for a stale semaphore and removes it if there is no MIS PID 
running.

2. On my system, shutting down the MIS daemon can take a long time and 
sometimes the process fails to stop after a SIGTERM. I wanted a way to 
forcefully terminate it using 'kill -9' after a period of time elapses. 
The script does this automatically after sixty seconds.

3. Other miscellaneous checks and balances - i.e. not attempting to stop 
the service if it isn't running, and to return proper error codes when 
exiting to indicate whether or not the process started/stopped 
successfully.

3. In addition to the MIS daemon, I also wanted to find a way to treat 
the mrc_client.py script used for the Multi Relay Chat (MRC) application in 
a similar way. You can find scripts and instructions for MRC at
https://vswitchzero.com/mrc-systemd

Files:

You can find the following files in the mystic-systemd.zip archive:

readme.txt - instructions similar to this web page.
mis-start.sh - the bash shell script to start the mis daemon.
mis-stop.sh - the bash shell script to stop the mis daemon.
mis.service - the systemd service file
file_id.diz - file descriptor

These scripts should work without modification (aside from a few 
variables) on any Debian based Linux distribution, including Ubuntu and 
Raspbian. I'd imagine they could run on CentOS, RHEL and others as well, 
but I have not tested them on other distributions. 

Instructions:

The instructions assume that you have a basic understanding of Linux. 
Depending on your system configuration, some customization may be necessary.

1. If running, stop your mystic daemon manually before following these 
instructions.

2. Extract mystic-systemd.zip to an accessible location. If you want to 
just use your home directory, you can run:

	unzip mystic-systemd.zip ~/
	cd ~/mystic-systemd

3. Edit the mis-start.sh and mis-stop.sh scripts using nano, vi or your 
favourite editor. You'll need to modify the variables at the top of each 
script. Namely the following:

	MIS_PATH=/mystic
	MIS_OPTS=daemon
	MIS_SHUT_OPTS=shutdown

MIS_PATH is the location of your mystic install. By default, it's set to 
/mystic. For MIS_OPTS, you'll want to use "daemon" unless you are 
running an older version of mystic. Older versions used "-d" instead. 

4. If you want to allow the script to forcefully shutdown MIS after 60 
seconds using 'kill -9', you'll need to uncomment the section of 
mis-stop.sh between the dashes. Simply remove the '#' character from 
each line of that section. Leaving it commented will cause the script to 
give up with an error code of 1 after 60 seconds of waiting for the 
process to stop gracefully. 

5. Copy the mis-start.sh and mis-stop.sh scripts to your mystic install 
directory. For example, if your mystic directory is /mystic, use:
	
	cp mis-start.sh /mystic
	cp mis-stop.sh /mystic

6. Ensure the scripts are executable. To be sure, you can run the 
following ((assuming they are in /mystic):

	chmod +x /mystic/mis-start.sh
	chmod +x /mystic/mis-stop.sh

7. Test the scripts from your mystic directory. I'd recommend ensuring 
your MIS daemon is stopped before testing. If you are not logged in as root, 
you'll need to use sudo. Try mis-start.sh first. If it's successful, you 
should see something like this:

	sudo ./mis-start.sh
	Attempting to start the Mystic Internet Service (mis)..
	No stale semaphore file found. Proceeding to start the mis daemon
	[MIS] Executing servers in DAEMON mode
	Checking to ensure the process starts..
	Finished! Mis daemon has been started successfully at PID 1613.

You'll know the script was successful if you get a '0' exit code from 
the following command:

	echo $?
	0

The mis-stop.sh script should output something like this:

	sudo ./mis-stop.sh
	Attempting to stop the Mystic Internet Service daemon..
	MIS daemon is currently running with PID 499. Stopping..
	[MIS] Note: This must be executed with SUDO or ROOT
	[MIS] Sending shutdown signal for PID 499
	Checking to ensure the process stops..
	MIS process still running. Waiting 5 seconds..
	Finished! MIS daemon has been stopped successfully.

If your process fails to stop gracefully, and you have uncommented the 
lines to forcefully stop it, you may see output similar to the 
following:

	sudo ./mis-stop.sh
	Attempting to stop the Mystic Internet Service daemon..
	MIS daemon is currently running with PID 1613. Stopping..
	[MIS] Note: This must be executed with SUDO or ROOT
	[MIS] Sending shutdown signal for PID 1613
	Checking to ensure the process stops..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	MIS process still running. Waiting 5 seconds..
	Error: Process failed to stop gracefully after 60 seconds.
	Stopping process forcefully using kill -9 
	Success. MIS daemon was forcefully stopped.

Note: The above is still considered a success with an exit code of '0', 
because in the end, the process was stopped.

8. Once you are confident your scripts are working correctly, you'll 
need to modify the mis.service file. Be sure to set your paths correctly 
in the two options shown below:

	ExecStart=/mystic/mis-start.sh
	ExecStop=/mystic/mis-stop.sh

9. Once done, copy the mis.service file to your /lib/systemd/system 
directory. Note that you'll need root for this, so use sudo if 
necessary:

	sudo cp mis.service /lib/systemd/system

10. Next, enable the service using systemctl. This will allow the 
service to start at bootup. You'll need root access for this as well:

	sudo systemctl enable mis.service


Using systemctl

Now that you've installed the mis.service file, you can start, stop and 
restart the Mystic daemon using the following commands:

	sudo systemctl start mis
	sudo systemctl stop mis
	sudo systemctl restart mis

If you want to prevent it from starting with the system, use 'disable' 
instead of 'enable'. 

	sudo systemctl disable mis.service

To get service status, run the following:

	sudo systemctl status mis
	systemctl status mis.service
	● mis.service - Mystic Internet Service
	Loaded: loaded (/lib/systemd/system/mis.service; enabled; vendor 
	preset: 
	enabled)
	Active: active (running) since Mon 2019-09-29 22:55:56 EDT; 
	10min ago
	Main PID: 1874 (mis)
	CGroup: /system.slice/mis.service
	└─1874 /mystic/mis daemon

The status output will also include the last ten lines of script output, 
which can be helpful for debugging purposes. Because of the way the 
scripts were designed, if it shows a green status, then the daemon 
should definitely be running.


Conclusion

I hope that other SysOps will find these scripts helpful. Please feel 
free to add to them or modify them for additional functionality. 




