Tags: scripts-and-functions cron hello-world
In the evening I like to watch TV while falling asleep. I usually start a shutdown process with the following command: sudo shutdown -h +45
or something slightly similar, which shutdowns the computer in 45 minutes.
I wanted to make this even easier, so I decided to create a small script which could automate this process.
Right click on your desktop a find create launcher
I added the following:
In e.g. Ubuntu 12.10 this menu item is not there any more, so there you will have to do a:
sudo gnome-desktop-item-edit /usr/share/applications/ --create-new
In the last case the launcher is placed in Applications -> other -> shutdown
. Then you can just drag it to the desktop.
The script /home/dennis/bin/shutdown.php
just creates a file called (in my case) /home/dennis/bin/.shutdown
. The script looks like the following the following, and it is created when I click the launcher:
#!/usr/bin/php
<?php
// create .shutdown file
// indicates that we will shut down
$path = realpath(__DIR__);
$check_file = "$path/.shutdown";
// creating file to indicate that we will shutdown
file_put_contents ($check_file, 'OK');
chmod the script to 777:
sudo chmod 777 shutdown.php
Now you can run the script by pressing the shutdown icon
. And see that it creates the .shutdown
file
Then I add a script (which will check for the .shutdown file, remove the .shutdown file, and then shutdown:
<?php
// checks if shutdown file exists
// that indicates that we will shut down
// if it exists we shutdown
$path = realpath(__DIR__);
$check_file = "$path/.shutdown";
if (file_exists($check_file)) {
// unlink file so that we does not shutdown
// again after every restart
$res = unlink($check_file);
if ($res) {
exec('sudo shutdown -h +45');
}
}
I placed this in /home/dennis/bin/shutdown_check.php
.
Then I added a cronjob to test if the .shutdown
file is created or not. It checks once every minute.
It looks like this:
* * * * * root php /home/dennis/bin/shutdown_check.php
And now I just need to click the launcher and my computer shutdowns after 45 minutes.
This page has been requested 4818 times