How could I replicate the functionality of this script in systemd?

Hi,

I have, in this 12.1 system, using systemv, a script at
“/etc/init.d/boot.local” that runs before syslog is active, and writes a
line to /var/log/messages:



> # syslog is not working
>
> DATE=`date  --rfc-3339=seconds`
> UNAME=`uname -a`
> echo >> /var/log/eventlog
> echo "$DATE - Booting the system now ================================================================================ $UNAME" | tee -a /var/log/warn | tee -a /var/log/eventlog >> /var/log/messages


I would like to have a similar thing using systemd (12.3). It simply
logs a line to the log file, directly, before the syslog is started, so
that I know at which precise instant the entries in the log pertain to a
new boot.

Likewise, I have “/etc/init.d/halt.local”, that runs after syslog has stop:



> DATE=`date  --rfc-3339=seconds`
> #UNAME=`uname -a`
> UPTIME=`uptime`
> echo "$DATE - Halting the system now =========================================== uptime: $UPTIME" | tee -a /var/log/warn | tee -a /var/log/eventlog >> /var/log/messages


How could I achieve the same thing with systemd? Remember, it has to run
while syslog is not running. Perhaps modifying the syslog startup module
itself? Inserting a new service?


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

This would be something like:

bor@opensuse:~> cat /etc/systemd/system/boot-marker.service 
[Unit]
Name=Write boot markers in /var/log/messages
Before=syslog.service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=-/bin/write-boot-marker start
ExecStop=-/bin/write-boot-marker stop

[Install]
WantedBy=multi-user.target
bor@opensuse:~> sudo systemctl enable /etc/systemd/system/boot-marker.service
ln -s '/etc/systemd/system/boot-marker.service' '/etc/systemd/system/multi-user.target.wants/boot-marker.service'
bor@opensuse:~> cat /bin/write-boot-marker 
#!/bin/sh

case "$1" in
    start )
        # syslog is not working

        DATE=`date  --rfc-3339=seconds`
        UNAME=`uname -a`
        echo >> /var/log/eventlog
        echo "$DATE - Booting the system now ================================================================================ $UNAME" | tee -a /var/log/warn | tee -a /var/log/eventlog >> /var/log/messages
    ;;
    stop )
        DATE=`date  --rfc-3339=seconds`
        #UNAME=`uname -a`
        UPTIME=`uptime`
        echo "$DATE - Halting the system now =========================================== uptime: $UPTIME" | tee -a /var/log/warn | tee -a /var/log/eventlog >> /var/log/messages
    ;;
esac

I also have a bash script useful in creating systemd service files with all of the various settings included in help files and you can set it to use any text editor you wish. It even starts you off with a default service file.

SysdCmd - systemd Command Help/Config Editor - Blogs - openSUSE Forums Thank You,

On 2013-05-25 06:46, arvidjaar wrote:
>
> This would be something like

(I have to read more)

It works!

I changed it a bit, moved the script to /usr/local/bin instead of /bin.

See the messages log:



> <3.6> 2013-05-25 15:15:23 eleanor3 systemd 1 - -  Stopping Login Prompts.
> <3.6> 2013-05-25 15:15:23 eleanor3 systemd 1 - -  Stopped target Login Prompts.
> <5.6> 2013-05-25 15:15:23 eleanor3 rsyslogd - - -  [origin software="rsyslogd" swVersion="7.2.7" x-pid="629" x-info="http://www.rsyslog.com"] exiting on signal 15.
> 2013-05-25 15:16:11+02:00 - Booting the system now  ================================================================================  Linux eleanor3.valinor 3.7.10-1.4-desktop #1 SMP PREEMPT Fri Apr 19 12:06:34 UTC 2013 (8ef74f8) x86_64 x86_64 x86_64 GNU/Linux
> <5.6> 2013-05-25 15:16:11 eleanor3 rsyslogd - - -  [origin software="rsyslogd" swVersion="7.2.7" x-pid="638" x-info="http://www.rsyslog.com"] start
> <0.6> 2013-05-25 15:16:11 eleanor3 kernel - - -     0.000000] Initializing cgroup subsys cpuset
> <0.6> 2013-05-25 15:16:11 eleanor3 kernel - - -     0.000000] Initializing cgroup subsys cpu

....

> <5.6> 2013-05-25 15:19:32 eleanor3 rsyslogd - - -  [origin software="rsyslogd" swVersion="7.2.7" x-pid="638" x-info="http://www.rsyslog.com"] exiting on signal 15.
> 2013-05-25 15:19:32+02:00 - Halting the system now  =========================================== uptime:  15:19pm  up   0:03,  0 users,  load average: 0.01, 0.03, 0.02
> 2013-05-25 15:20:33+02:00 - Booting the system now  ================================================================================  Linux eleanor3.valinor 3.7.10-1.4-desktop #1 SMP PREEMPT Fri Apr 19 12:06:34 UTC 2013 (8ef74f8) x86_64 x86_64 x86_64 GNU/Linux
> <5.6> 2013-05-25 15:20:33 eleanor3 rsyslogd - - -  [origin software="rsyslogd" swVersion="7.2.7" x-pid="649" x-info="http://www.rsyslog.com"] start


At the exact place :-)))

Thanks!

I combine this with another script in “/etc/pm/sleep.d/1things”:



> #!/bin/bash
>
> . /usr/lib/pm-utils/functions
>
> NUM=1
>
> DATE=`date  --rfc-3339=seconds`
>
> case "$1" in
>         hibernate)
>                 /bin/logger -t pm-utils -p syslog.warn "Hibernating ($NUM)..."
>                 echo "$DATE - hibernating the system now" >> /var/log/eventlog
>                 ;;
>         suspend)
>                 /bin/logger -t pm-utils -p syslog.warn "Suspending ($NUM)..."
>                 echo "$DATE - suspending the system now" >> /var/log/eventlog
>                 ;;
>         thaw)
>                 /bin/logger -t pm-utils -p syslog.warn "Thawing ($NUM)..."
>                 echo "$DATE - thawing the system now" >> /var/log/eventlog
>                 ;;
>         resume)
>                 /bin/logger -t pm-utils -p syslog.warn "Resuming ($NUM)..."
>                 echo "$DATE - resuming the system now" >> /var/log/eventlog
>                 ;;
>         *)
>                 ;;
> esac


So that I have a log of booting, halting, hibernation… of the machine.

However, a side effect of hibernating the machine from the text console
by issuing “pm-hibernate” (it is a virtual machine running under
vmplayer) has the side effect that the warning messages that were going
to the tty10 now go to tty1 instead. :-?

That happened to me long time ago, the solution then was


klogconsole -r 10

I’ll have to see if that works, and if the real system needs it.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

On 2013-05-25 15:26, jdmcdaniel3 wrote:
>
> I also have a bash script useful in creating systemd service files with
> all of the various settings included in help files and you can set it to
> use any text editor you wish. It even starts you off with a default
> service file.
>
> 'SysdCmd - systemd Command Help/Config Editor - Blogs - openSUSE Forums
>
> ’ (http://tinyurl.com/bovchgx) Thank You,

Interesting! I’m downloading it now. Testing… Heh, seen via ssh the
background is white, as my xterm default.

Nice program!

Heh, testing it, just found a bug: it ignores the EDITOR setting, it
uses vi as an editor instead of the configured one:


eleanor3:~ # cat .bashrc
#export EDITOR=/usr/bin/mcedit
export EDITOR=/usr/bin/jstar
eleanor3:~ # echo $EDITOR
/usr/bin/jstar
eleanor3:~ #

If I use “crontab -e”, it uses jstar as editor. But not “SysdCmd”.
Looking at the code, I see you hardcode it. :-??

Editing that works, of course.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

The selected editor depends on the detected desktop and I would love to make sure that works properly in SysdCmd. What desktop are you using?

Thank You,

On 2013-05-25 16:16, jdmcdaniel3 wrote:

> The selected editor depends on the detected desktop and I would love to
> make sure that works properly in SysdCmd. What desktop are you using?

None, an ssh session on xterm, thus text mode, which means that it
selects vi. However, there is an environment variable where the user can
define his proffered editor for text mode:


eleanor3:~ # echo $EDITOR
/usr/bin/jstar

If the variable does not exist or is empty, you use defaults. If it
exists, you use its contents :slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

I have not recently seen a system that by default has this set and mostly I am not targeting Terminal with no Desktop loaded, but with the option to set it as you desire. At least its not a bug exactly in the bash script, but I could ask it to look for this value first.

Thank You,