LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Executing command on systemd poweroff/reboot (https://www.linuxquestions.org/questions/linux-newbie-8/executing-command-on-systemd-poweroff-reboot-4175471104/)

Mr. Alex 07-27-2013 03:43 AM

Executing command on systemd poweroff/reboot
 
Hello LQers! Long time no visit. I reinstalled my Arch system and now experiencing the death of GNU/Linux as a normal and useful operating system. It went to barely usable garbage. I hate it now and I am severely disappointed in nearly all the development processes going on in the community. Anyway, with this new garbage-dee thing I can't figure out how to execute random command on reboot/poweroff (automatically, as root), just like I did it with echoing string to "/etc/rc.local.shutdown". Can anyone please just tell me? Because I tried to search and get suggestions like

Code:

[Unit]
Description=Console System Reboot Logging
DefaultDependencies=no
After=sysinit.target console-kit-log-system-start.service
Before=shutdown.target

[Service]
Type=oneshot
ExecStartPre=/bin/bash -c /home/domac/.bin/dwm-tmpfile store
ExecStart=/usr/sbin/ck-log-system-restart

I don't feel like digging any deeper... :D That is just a joke! I feel sick trying to fathom this.

TRUNoise 07-27-2013 08:10 PM

A cron job could fit the job of doing something at startup (ie. stage 2 of a reboot, turning on again)

To add these cron jobs to the system, go to the console and do the following:
Code:

crontab -e
then at the bottom of the file gets opened, put the commands as shown below in there on the bottom of the file (where the hashtags end).

Quoting from the Cron Wikipedia page:
Quote:

The following line specifies that the Apache error log clears at one minute past midnight (00:01) of every day of the month, of every day of the week, assuming that the default shell for the cron user is Bourne shell compliant:
Code:

1 0 * * *  printf > /var/log/apache/error_log
The following line makes the user program test.pl—ostensibly a Perl script—run every two hours, at midnight, 2am, 4am, 6am, 8am, and so on:
Code:

0 */2 * * *  /home/username/test.pl

Here's how the timings work:
Quote:

* * * * * command to execute
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names)
│ │ │ └────────── month (1 - 12)
│ │ └─────────────── day of month (1 - 31)
│ └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)
So, for running a cron on boot, instead of using the asterisks, you would use @reboot in a fashion like
Code:

@reboot [what to do]
So for instance to execute a sh file on startup, do
Code:

@reboot sh /home/Alex/Desktop/script.sh
For more info on cron, have a look at the Wikipedia page.

As for the shutdown part of your question, you could just have a .sh file somewhere on your computer containing
Code:

[stuff to do, ie. echo Goodbye!]
sudo shutdown -r now

then run that whenever you needed to reboot or if you needed to shutdown, replace the "shutdown -r now" part with "poweroff"

Hope I helped. :)

Mr. Alex 07-28-2013 04:20 AM

Yes, thanks, I didn't think of that solution.


All times are GMT -5. The time now is 09:22 PM.