4

I've recently setup Fedora 22 onto a machine and it's using the systemd init system.

I've been reading up on it and now I need to create a systemd start up script for postgresql

For testing I created the following shell script, hello_world

#! /bin/sh
#  testing systemctl script

start() {
   echo "Executing Start"
   echo "Testing 01"
   echo "Testing 02"
   echo "Testing 03"
}

stop() {
   echo "Stopping Hello World Script"
}

case "$1" in
   start)
      start
   ;;
   stop)
      stop
   ;;
   restart)
      stop
      sleep 2
      start
   ;; 
   *) exit 1
esac

Which when I run it using the terminal it does what I expect and echos the strings

. hello_world start

This echos "Starting Hello World Script" then I placed it inside /usr/lib/systemd/scripts

Then I tried to create the systemd service script as follows, hello_world.service

[Unit]
Description=Hello World Testing Script

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/hello_world start
ExecStop=/usr/lib/systemd/scripts/hello_world stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Which I placed inside /usr/lib/systemd/system and tried to execute with

systemctl start hello_world.service

Which did not give any errors but I did not get the echo string that I expected when i executed the hello_world script alone.

So I can't tell if it is actually working, did I miss something? why is the systemctl command not echoing the string from the script?

  • systemd is not a en.wikipedia.org/wiki/File_system. I'm surprised the postgresql package doesn't include systemd scripts. – Lior Silberman Aug 6 '15 at 17:22
  • 3
    It does echo your messages but not where you expect it as the log target is journal. If you run journalctl -f in a terminal and then start your service in another terminal you will see in the first terminal: systemd[1]: Starting Hello World Testing Script... hello_world[5038]: Starting Hello World Script systemd[1]: Started Hello World Testing Script. where hello_world[5038]: Starting Hello World Script is your actual script echoing the message. – don_crissti Aug 6 '15 at 17:54
  • 1
    @don, thanks for the info don, is there a way to echo the message to the terminal when using systemctl because it's a bit of a pain to keep checking log files – TheLovelySausage Aug 6 '15 at 20:17
  • 1
    You don't need to install the postgresql with dnf. Just do wget https://mirror.umd.edu/fedora/linux/releases/22/Server/x86_64/os/Packages/p/postgresql-server-9.4.1-1.fc22.x86_64.rpm and then rpm2cpio postgresql-server-9.4.1-1.fc22.x86_64.rpm | cpio -i --to-stdout "*postgresql.service" > postgresql.service – Lior Silberman Aug 7 '15 at 17:14
  • 1
    @Trent It seems you found the answer, but edited it into the question. You should write a brief answer, accept it, and take the answer snippet out of your question. – Centimane Apr 27 '17 at 11:43
1

It seems that the output for the script is not sent to the terminal it is instead redirected to the status command

After the service is running

# systemctl start hello_world.service

I can view my outputs in the status

# systemctl status hello_world.service

Which will include any outputs from the script like bellow

Apr 28 10:18:00 centos7.adminserv systemd[1]: Starting Hello World Testing S....
Apr 28 10:18:00 centos7.adminserv hello_world[18164]: Executing Start
Apr 28 10:18:00 centos7.adminserv hello_world[18164]: Testing 01
Apr 28 10:18:00 centos7.adminserv hello_world[18164]: Testing 02
Apr 28 10:18:00 centos7.adminserv hello_world[18164]: Testing 03
Apr 28 10:18:00 centos7.adminserv systemd[1]: Started Hello World Testing Sc.... 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.