Bash Unit File
git clone https://github.com/LaunchCodeTechnicalTraining/bash-garbage-collector
Review Source Code
#!/bin/bash dir=/home/student/bash-garbage-collector/trash log=/home/student/bash-garbage-collector/garbage.log while true do if [[ $(ls $dir) == "" ]] then echo "$(date): no contents detected in $dir" >> $log else echo "$(date): contents detected in $dir...emptying" >> $log rm $dir/* fi sleep 30 done
A simple script that checks the contents of
/home/student/bash-garbage-collector/trash every 30 seconds. If contents exist at the location it logs that contents were detected and deletes the contents. If contents were not detected it simply logs that no contents were found.
From the project directory run:
Feel free to add files to
/home/student/bash-garbage-collector/trash after 30 seconds check the contents of
/home/student/bash-garbage-collector/garbage.log and the
trash/ directory to see the effects of the script.
The script can be stopped by entering
c to send the interrupt signal to the running process.
Create Unit File
This bash script can be converted into a
systemd service by creating a valid unit file. After creating the unit file the service will be controllable with the
Create a file in
bash-garbage-collector.service as the
sudo touch /etc/systemd/system/bash-garbage-collector.service
Add the following contents to the file using your editor of choice (make sure you are still acting as the
[Unit] Description=An awesome bash garbage collector (files in a certain directory) [Service] ExecStart=/usr/bin/bash /home/student/bash-garbage-collector/empty.sh Restart=never [Install] WantedBy=multi-user.target
- Description: the human readable description of the service
- ExecStart: The command invoked when the service is started (in this case
/usr/bin/bashis being used to execute
- Restart: Instructions on how and when the service should restart (in this case never).
- WantedBy: The system level in which the service should be initialized if
enabled(in this case just before the user login screen appears (a multi-user environment has been reached))
Service Control via
sudo systemctl start bash-garbage-collector
After starting the service add contents to
/home/student/bash-garbage-collector/trash wait 30 seconds and see how the directory and the
garbage.log are affected.
sudo systemctl stop bash-garbage-collector
After stopping the service the garbage collector is no longer running.
sudo systemctl enable bash-garbage-collector
After enabling the service the garbage collector should start when the machine reboots.
You can reboot your virtual machine in many ways. The easiest is with the
reboot command. After rebooting check the status of the service, and look over the contents of the
sudo systemctl disable bash-garbage-collector
After disabling the service it will no longer start when the computer starts.
Right now the service file instructs
systemd to never restart the running service. Even if the service fails and stops running
systemd will leave it in a stopped state.
This can be tested by starting, and finding the
PID of the service:
systemctl status bash-garbage-collector
Main PID: 556 (bash)
Simulate Failure with
PID (process id) will be a different number on your virtual machine. When entering the next command make sure to use the
PID of your specific service file!
We can simulate a failure by sending a
SIGKILL signal with the
sudo kill -9 [YOUR-SERVICE-PID]
After simulating a catrastophic failure check the status of the service:
systemctl status bash-garbage-collector
Configure Service to Restart on Failure
systemd can be told to restart a service when it fails. Change the
Restart section of the
/etc/systemd/system/bash-garbage-collector.service file to:
systemd knows to restart the service when it has experienced a failure.
Stop and Start Service
After making a change to a unit file you will need to stop and start the service again.
In some instances, depending on the state of the service when a change was made to the service’s underlying unit file, the
systemd tool may need to be restarted. If this is required your terminal will let you know, and will ask you to execute a command similar to:
sudo systemctl daemon-reload
If you see this, make sure to do it so that the change to the underlying unit file is loaded into
PID of the service again and kill it with a
SIGKILL signal (
Check the status of the service. Take note of the new
PID the service was assigned when it was restarted.