RHEL 7 how run longtime script before shutdown/reboot systemd

6,426

Try changing the TimeoutStopSec value in the unit file. The default is set by /etc/systemd/system.conf in the DefaultTimeoutStopSec entry (my system has it as 90s). You can set TimeoutStopSec=0 to disable the timeout entirely (may hang your shutdown if the script doesn't terminate), or pass a larger value that allows the stop script to finish completely.

Share:
6,426

Related videos on Youtube

Ivo Jedinečný
Author by

Ivo Jedinečný

Updated on September 18, 2022

Comments

  • Ivo Jedinečný
    Ivo Jedinečný over 1 year

    I'm trying to found solution how run script before shutdown/reboot is initiated in RedHat7. I need to correctly shutdown SAP database when I'm shuting down or rebooting server.

    Script for SAP shutdown take 3-4minutes, but shutdown of system is very quick -it mean that RedHat kill all proceses imediatly...

    my

    systemd sap.service is:

    [Unit]
    Description=Shutdown SAP
    Before=shutdown.target reboot.target halt.target
    
    [Service]
    ExecStart=/bin/true
    ExecStop=/usr/sap/stopsap
    RemainAfterExit=true
    KillMode=none
    
    [Install]
    WantedBy=multi-user.target
    

    As I see in shutdown log - database wasnt shutted down correctly:

    Checking ADA Database
    -------------------------------------------
    setTrace: false
    J2EE Database is not available
    

    Can you help me?

    Thank you

    Ivo

    Edit 21.10.2015:

    I moved to next step - it is working better, but still not correctly:

    [Unit] Description=SAP sluzba After=network.target sshd.target Wants=network.target sshd.service [Service] Type=simple RemainAfterExit=true Environment="SAPSYSTEMNAME=RH7" "HOST=cz-brn1-rh7" "HOME=/home/rh7adm" "PATH=/sapdb/clients/RH7/bin:/sapdb/programs/bin:/usr/lib64/qt-.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/sap/RH7/SYS/exe/uc/linuxx86_64:/usr/sap/RH7/SYS/exe/run:/home/rh7adm:." "DIR_LIBRARY=/usr/sap/RH7/SYS/exe/run" "LD_LIBRARY_PATH=/usr/sap/RH7/SYS/exe/run:/usr/sap/RH7/SYS/exe/uc/linuxx86_64:/sapdb/clients/RH7/lib" "RSEC_SSFS_DATAPATH=/usr/sap/RH7/SYS/global/security/rsecssfs/data" "RSEC_SSFS_KEYPATH=/usr/sap/RH7/SYS/global/security/rsecssfs/key" KillMode=none SendSIGKILL=no TimeoutSec=5min TimeoutStopSec=5min User=rh7adm Group=sapsys ExecStart=/usr/sap/startsap ExecStop=/usr/sap/stopsap
    [Install] WantedBy=graphical.target

    Here is stopsap script:

    #!/bin/bash date >> /usr/sap/stopsap.log; /usr/sap/RH7/SYS/exe/uc/linuxx86_64/stopsap >> /usr/sap/stopsap.log 2>&1; whoami >> /usr/sap/stopsap.log;

    J2EE Database is running See logfile /home/rh7adm/JdbcCon.log stopping the SAP instance J28 Shutdown-Log is written to /home/rh7adm/stopsap_J28.log /usr/sap/RH7/J28/exe/sapcontrol -prot NI_HTTP -nr 28 -function Stop Instance on host cz-brn1-rh7 stopped Waiting for cleanup of resources..................................................................................................................................................

    and here it stuck for that 5 minutes.. Shutdown script never finish:(

    Can I trace how gone shutting down of my sap. service?

    Thank you..

    • Evgeny Vereshchagin
      Evgeny Vereshchagin over 8 years
      hm, ExecStart=/bin/true. How do you start your database?
    • Ivo Jedinečný
      Ivo Jedinečný over 8 years
      Hi, I'm always start database manually with SAP.
    • Evgeny Vereshchagin
      Evgeny Vereshchagin over 8 years
      Why do you start it manually? Is SAP a doubleforking process? You can set ExecStart=cmd-to-start and remove Before=. Unless DefaultDependencies= is set to false, service units will implicitly have dependencies of type Requires= and After= on basic.target as well as dependencies of type Conflicts= and Before= on shutdown.target. These ensure that normal service units pull in basic system initialization, and are terminated cleanly prior to system shutdown.
    • Ivo Jedinečný
      Ivo Jedinečný over 8 years
      I tried, it without Before and with ExecStart=/usr/sap/startsap but without success. su[715]: (to rh7adm) root on none Oct 09 11:26:19 cz-brn1-rh7 su[715]: pam_systemd(su-l:session): Failed to create session: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken. scripts for startsap or stopsap cannot be run as root but as sapadm (in this case rh7adm) also tried with DefaultDependencies=false still without success...
    • Evgeny Vereshchagin
      Evgeny Vereshchagin over 8 years
      Try the directive User=. Is /usr/sap/startsap a shell script? can you post it?
    • Evgeny Vereshchagin
      Evgeny Vereshchagin over 8 years
      anyway, your question is how to write a service unit for sap:) not how run longtime script before shutdown/reboot systemd:)
    • Ivo Jedinečný
      Ivo Jedinečný over 8 years
      I'm sorry for that - till Rhel 6,5 we used initab for this.. ;) script for startsap is su - rh7adm -c "startsap" >> /usr/sap/startsap.log 2>&1 rh7adm is sapsystem user
  • Ivo Jedinečný
    Ivo Jedinečný over 8 years
    Also I found this (i tried values 250 or 0, still same): sap.service - Shutdown SAPu Loaded: loaded (/usr/lib/systemd/system/sap.service; enabled) Active: active (exited) since Fri 2015-10-09 10:13:16 CEST; 2min 0s ago Main PID: 715 (code=exited, status=0/SUCCESS) CGroup: /system.slice/sap.service Oct 09 10:13:16 cz-brn1-rh7 systemd[1]: Started Shutdown SAPu. Oct 09 10:15:16 cz-brn1-rh7 systemd[1]: [/usr/lib/systemd/system/sap.service:5] Unknown lvalue 'TimeoutStopSec' in section 'Unit
  • Ivo Jedinečný
    Ivo Jedinečný over 8 years
    And by the way: /etc/systemd/system.conf are all values commented: #TimerSlackNSec= #DefaultTimeoutStartSec=90s #DefaultTimeoutStopSec=90s #DefaultRestartSec=100ms
  • Tom Hunt
    Tom Hunt over 8 years
    The commented values in /etc/systemd/system.conf denote the defaults. If a value there is commented, then it's probably the one in effect through systemd's compiled-in behavior.
  • Tom Hunt
    Tom Hunt over 8 years
    TimeoutStopSec goes under the [Service] heading, not [Unit].
  • Ivo Jedinečný
    Ivo Jedinečný over 8 years
    Ok, I tried TimeoutStopSec directive, but with no success - database is already killed when script trying to shutdown DB.