I'd like to run a .sh file using bash on logout and shutdown of Ubuntu. I have searched up and down the web and have found only fixes for KDE and GNOME. The script is simple, I just need to know if I can add the line of code to the "logoff/shutdown" file or if I need to reference it in another script.
|
|
||||
To execute a script upon logout:Add the following line:
in You have to restart lightdm in order that this change to take effect. To do this go in tty1 using Ctrl+Alt+F1, login with your username and password and run the following command:
Note: use Ctrl+Alt+F7 to switch to the graphical session To execute a script upon reboot:
To execute a script at shutdown:
Sources: |
|||||||||||||||||||||
|
How to do it with systemdIf you find this thread after June 2016, Ubuntu and its derivatives including Mint will be using systemd to control system initialization and shutdown. I had problems with the old approach so researched the systemd way. With systemd you create one or two files to call your scripts using the templates below, and execute a command. Simple. GUI VersionFirst create the scripts you want to run at startup and/or shutdown. 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:
Then I opened my file manager as root, opened
and
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 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. CLI (Command Line) VersionThis 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 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 showed some alternatives at Stack Exchange.
and copy in the file content:
Then Enable the Service with the command:
and reboot your system after which the services will be active. The commands Changing the Trigger Conditions for ShutdownThe 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 an answer at Unix & Linux by @Matthias but I was unable to get it to run until I added an Install section. Again, edit the script in
|
|||||||||||||||||||||
|
systemd) so people would see it on top. – Jonathan Y. Nov 25 '17 at 22:24