6

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?

10
  • 4
    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. Aug 6, 2015 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 Aug 6, 2015 at 20:17
  • 2
    If you want to see output in console (VT) add StandardOutput=tty to your [Service] section. If you just want to play with it in the current terminal then run tty (should return something like /dev/pts/2) and add a second line TTYPath=/dev/pts/2 under StandardOutput=tty; after editing the service file run systemctl daemon-reload and then starting/stopping your service should echo to current terminal... Aug 6, 2015 at 20:44
  • 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 Aug 7, 2015 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, 2017 at 11:43

1 Answer 1

2

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.... 
1
  • That is just showing you relevant lines from journal. It's the same thing if you did journalctl -f just filtered specifically for that unit file, which you can also do using journalctl command if you wanted.
    – fred
    Mar 27, 2021 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .