LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2015, 02:44 PM   #1
jzoudavy
Member
 
Registered: Apr 2012
Distribution: Ubuntu, SUSE, Redhat
Posts: 180

Rep: Reputation: Disabled
systemd starting services


hi all

I am learning systemd and how to add new services as part of the LFS201 course and I have a question about the services:
Code:
Lab 4.2: Adding a New Startup Service with systemd
For example a very minimal file named
/etc/systemd/system/fake2.service:
[Unit]
Description=fake2
After=network.target
[Service]
ExecStart=/bin/echo I am starting the fake2 service
ExecStop=/bin/echo I am stopping the fake2 service
[Install]
WantedBy=multi-user.target
Code:
root@ubuntu:/etc/systemd/system# systemctl start fake.service
root@ubuntu:/etc/systemd/system# systemctl status fake.service
 fake.service - fake
   Loaded: loaded (/etc/systemd/system/fake.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

May 16 11:41:05 ubuntu systemd[1]: Started fake.
May 16 11:41:05 ubuntu systemd[1]: Starting fake...
May 16 11:41:05 ubuntu echo[1798]: I am starting the fake2 service
May 16 11:41:05 ubuntu echo[1800]: I am stopping the fake2 service
root@ubuntu:/etc/systemd/system# ps aux | grep fake*
root      1809  0.0  0.0  13688  2272 pts/8    S+   11:41   0:00 grep --color=auto fake.service
root@ubuntu:/etc/systemd/system#
as you can see the fake2 service is really only two lines. And when I grep for the service via ps I can't fine it. I guess it is because it has finished running. I am wondering how can I change it so that I can keep it running?

thanks
 
Old 05-16-2015, 04:00 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Replace /bin/echo with a script that has a long sleep after doing the echo...

I believe you will also have to use a different script for the stop command or just ignore it as the echo shows it would have run. But if you actually want it to do something, you would need to record the other process id somewhere to kill it.

Something like:
Code:
#!/bin/bash
# example service script
echo $*

echo $$ >/run/your_pid_file
while [ 1 ]; do
   sleep 30
done
and to stop it:

Code:
#!/bin/bash

echo $*
pid_val=$(cat /run/your_pid_file)
kill -3 $pid_val
sleep 5
kill -9 $pid_val

Last edited by jpollard; 05-16-2015 at 04:06 PM.
 
Old 05-16-2015, 07:33 PM   #3
Head_on_a_Stick
Senior Member
 
Registered: Dec 2014
Location: London, England
Distribution: Arch & Debian
Posts: 1,183

Rep: Reputation: 283Reputation: 283Reputation: 283
Quote:
Originally Posted by jzoudavy View Post
I guess it is because it has finished running. I am wondering how can I change it so that I can keep it running?
Try this:
Code:
[Unit]
Description=fake2
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/echo "I am starting the fake2 service"
ExecStop=/bin/echo "I am stopping the fake2 service"
[Install]
WantedBy=multi-user.target
Code:
empty@Arch ~ % sudo systemctl start fake2
empty@Arch ~ % systemctl status fake2
● fake2.service - fake2
   Loaded: loaded (/etc/systemd/system/fake2.service; disabled; vendor preset: disabled)
   Active: active (exited) since Sun 2015-05-17 00:32:34 BST; 3s ago
  Process: 5965 ExecStart=/bin/echo I am starting the fake2 service (code=exited, status=0/SUCCESS)
 Main PID: 5965 (code=exited, status=0/SUCCESS)

May 17 00:32:34 Arch systemd[1]: Starting fake2...
May 17 00:32:34 Arch echo[5965]: I am starting the fake2 service
May 17 00:32:34 Arch systemd[1]: Started fake2.
empty@Arch ~ % sudo systemctl stop fake2
empty@Arch ~ % systemctl status fake2   
● fake2.service - fake2
   Loaded: loaded (/etc/systemd/system/fake2.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

May 17 00:32:34 Arch systemd[1]: Starting fake2...
May 17 00:32:34 Arch echo[5965]: I am starting the fake2 service
May 17 00:32:34 Arch systemd[1]: Started fake2.
May 17 00:32:50 Arch systemd[1]: Stopping fake2...
May 17 00:32:50 Arch echo[6006]: I am stopping the fake2 service
May 17 00:32:50 Arch systemd[1]: Stopped fake2.
Seems to work...

Last edited by Head_on_a_Stick; 05-16-2015 at 07:43 PM. Reason: removed extraneous `status` lines
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] systemd incursion! ( fix X starting on same vt ) Keith Hedger Linux From Scratch 13 04-30-2015 03:09 PM
[SOLVED] Starting EyeFi server daemonized with systemd mr_minning CentOS 2 09-26-2014 12:17 PM
When Centos 6.3 starting,All services starting OK and starting Atd: OK .And stop scr. DeSouffle Linux - General 4 08-29-2013 01:46 AM
LXer: Managing Services on Linux with systemd LXer Syndicated Linux News 0 12-20-2011 05:00 PM
LXer: Managing Services on Linux with systemd LXer Syndicated Linux News 0 12-20-2011 12:50 PM


All times are GMT -5. The time now is 10:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration