4

I have the following Systemd service script to run a Spring boot application-

[Unit]
Description=Upstart for Security
After=network.target network-online.target
Wants=network-online.target

[Service]
User=root
WorkingDirectory=/home/ubuntu/security
ExecStart=/usr/bin/java -classpath java -Dspring.profiles.active=stage -jar /home/ubuntu/security/security-0.0.1-SNAPSHOT.jar > /home/ubuntu/security/security.log 2>&1
SuccessExitStatus=143
Restart=always
RestartSec=120s

[Install]
WantedBy=multi-user.target


I save the script in the following location -

 /etc/systemd/system


I ran the following commands to run the systemd service script -

1. sudo systemctl enable security.service -or- sudo systemctl daemon-reload
2. sudo systemctl status security.service 
3. sudo systemctl start security.service


To check logs, I fire the command -

journalctl -u security.service

and use SHIFT+G to scroll to eof

I am able to check the logs by the above steps, but I want them in an external file in location /home/ubuntu/security , as security.log
How can I achieve it? What change do I make in my systemd script?

2
  • 1
    This line /home/ubuntu/security/security.log 2>&1 won't run as this is not a shell script as I have learnt, but for re-direct I haven seen such but still looking Apr 27, 2017 at 18:18
  • yea, it worked with Upstart but no luck with systemd as far as custom log file is concerned :'(
    – Dev1ce
    Apr 27, 2017 at 18:20

1 Answer 1

4

systemd already provides a custom log for you by default in the systemd journal. It automatically redirects both STDIN and STDOUT there either as well as handling log file growth and rotation.

Just leave the log file redirection out of your ExecStart= line. To view the logs generated by your service, use journalctl -u security.

See man journald.conf for related configuration options and man journalctl for more options for viewing and querying the log data.

The docs in man systemd.service explain that the redirection syntax is not supported in the ExecStart= line:

This syntax is intended to be very similar to shell syntax, but only the meta-characters and expansions described in the following paragraphs are understood. Specifically, redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported.

2
  • My problem is, when we had an Upstart in place we were able to create a separate log file and when we faced any problems, we could download that log file locally and send to respective team. So how do we do that with Systemd ?
    – Dev1ce
    Apr 28, 2017 at 4:28
  • You can always export a copy of the log when needed with journalctl -u security > /path/to/security.log. Make sure in /etc/systemd/journald.conf that you have Storage=persistent to make sure that logs are perserved between boots. Apr 28, 2017 at 13:51

You must log in to answer this question.

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