How to Do it with Systemd (it's easier)
Now that the Ubuntu variants and Mint have moved to systemd I found my old solutions based on the above to be less satisfactory. I searched around the web to find out how to do it with systemd and ended up combining others' wisdom and documenting it as a blog post at blogspot.com.au containing the following tutorial.
With systemd you create one or two files to call your scripts using the templates below, and execute a couple of commands. Simple.
First create the scripts you want to run at startup and/or shutdown. Just do one if you don't require both. I created .scopening_atstart and .scfullcopy_atend.
Then make sure that they are both executable by right clicking the file, selecting properties and making sure that, under permissions, you have ticked Allow executing file as a program.
The two files I created populate and save the contents of a ramdisk. They also create a file in my home directory to prove that the service is working. They were of the form:
cp -pru /home/john/zRamdisk/subdirectory1/* /home/john/.wine/drive_c/subdirectory1/
date +%D' '%T > /home/john/stop_time
Then I opened my file manager as root, opened
/etc/systemd/system and created a file startup.service and a file save-ramdisk.service. Obviously you can choose your own names and generic names could have included a startup file called johns_start.service and a shutdown file called johns_shutdown.service. Just don't pick existing service names.
Description=Save Ramdisk to Wine drive C
You can use the same service files, substituting the full path of your executable script for mine.
Finally for each one execute the command systemctl enable your_files_name (but without the suffix service). So my first one was
systemctl enable startup
Reboot the computer once to start the services. The start service will be executed whenever systemd enters the multi-user target and the stop service when it exits the multi-user target. Alternative service files with different activation conditions will be described below.
Additional useful commands include:
systemctl is-enabled startup
systemctl is-active startup
systemctl restart startup
More can be found at the reference above.
CLI (Command Line) Version
This description assumes that you operate from your home directory rather than /home/john, use sudo as needed, and your choice of editor where I write vim or svim.
Create startup and shutdown shell scripts with the first line
#!/bin/sh and make them executable using
chmod +x my_new_filename.
Create two files as above, or in this example, one file to handle startup and shutdown tasks. I will execute scripts in my home directory but @don_crissti shows some alternatives at Stack Exchange.
and copy in the file content:
Description=Run Scripts at Start and Stop
ExecStart=/home/john/.startup_commands #your paths and filenames
Then Enable the Service with the command:
systemctl enable start_and_stop
and reboot your system after which the services will be active. The commands
systemctl is-enabled start_and_stop and
systemctl is-active start_and_stop can be used in monitoring your new services.
Changing the Trigger Conditions for Shutdown
The files above all use the open or close of the multi-user environment to initiate running the scripts. The file below uses the beginning of four potential shutdown processes to initiate its scripts. Adding or removing the targets on the Before line + the WantedBy line will let you make finer distinctions:
This file was proposed in the second answer of this post but I was unable to get it to run until I added an Install section.
Again, edit the script in
/etc/systemd/service/ and enable it using
systemctl enable your_file_name. When I changed the targets I used the
systemclt disable file_name command and then re-enabled it which symlinked it to the target directories. Reboot and the service will be operating.
Description=Do something required
Before=shutdown.target reboot.target halt.target
# This works because it is installed in the target and will be
# executed before the target state is entered
# Also consider kexec.target
ExecStart=/home/john/.my_script #your path and filename
WantedBy=halt.target reboot.target shutdown.target