check enabled systemctl service

291 views
Skip to first unread message

jehan procaccia

unread,
Jul 25, 2013, 5:37:48 AM7/25/13
to help-c...@googlegroups.com
hello
I want to check if a service is enabled .
on fedora19 (and since 15 I think ) to enable a service we use systemctl

# systemctl status autofs.service
autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled)

# systemctl enable autofs.service
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'

now on next boots autofs will be running

I don't see which combination of "service", "command",  "process" I should use to enable a service only if it was disabled  ?
thanks for your help

Trond Amundsen

unread,
Jul 25, 2013, 6:55:18 AM7/25/13
to help-c...@googlegroups.com

 Don't use "status" for this. What I'm using is (simplified):

classes:
 
"link_autofs"
     
not => returnszero("/usr/bin/systemctl -q is-enabled autofs.service","noshell");

commands
:
  link_autofs
::
   
"/usr/bin/systemctl -q enable autofs.service";

Systemd makes cfengine code simpler because systemctl is a far better tool than chkconfig and the old SysV scripts :) To start autofs if it isn't running using only systemctl:

classes:
 
"start_autofs"
     
not => returnszero("/usr/bin/systemctl -q is-active autofs.service","noshell");

commands
:
  start_autofs
::
   
"/usr/bin/systemctl start autofs.service";

-trond

jehan procaccia

unread,
Jul 25, 2013, 11:57:24 AM7/25/13
to help-c...@googlegroups.com

Thanks, it looks good and seems to work fine copying as is your promise above !

2013-07-25T17:55:09+0200  verbose: Promiser string contains a valid executable '/usr/bin/systemctl' - ok
2013-07-25T17:55:09+0200     info: Executing 'no timeout' ... '/usr/bin/systemctl -q enable autofs.service'
2013-07-25T17:55:09+0200  verbose: (Setting umask to 77)
2013-07-25T17:55:09+0200  verbose: /disi_policies/commands/'/usr/bin/systemctl -q enable autofs.service': Additional promise info: version 'Community Promises.cf 3.4.0' source path '/var/cfengine/inputs/cf-disi-policies.cf' at line 43
2013-07-25T17:55:09+0200  verbose: /disi_policies/commands/'/usr/bin/systemctl -q enable autofs.service': Finished command related to promiser '/usr/bin/systemctl -q enable autofs.service' -- succeeded
2013-07-25T17:55:09+0200     info: Completed execution of '/usr/bin/systemctl -q enable autofs.service'

# /usr/bin/systemctl is-enabled autofs.service
enabled

very clever

thanks !

jehan procaccia

unread,
Jul 25, 2013, 12:08:11 PM7/25/13
to help-c...@googlegroups.com

And the same is true for restarting the service is it is disabled

 2013-07-25T18:02:14+0200  verbose: Promiser string contains a valid executable '/usr/bin/systemctl' - ok
2013-07-25T18:02:14+0200     info: Executing 'no timeout' ... '/usr/bin/systemctl start autofs.service'
2013-07-25T18:02:14+0200  verbose: (Setting umask to 77)
2013-07-25T18:02:15+0200  verbose: /disi_policies/commands/'/usr/bin/systemctl start autofs.service': Additional promise info: version 'Community Promises.cf 3.4.0' source path '/var/cfengine/inputs/cf-disi-policies.cf' at line 45
2013-07-25T18:02:15+0200  verbose: /disi_policies/commands/'/usr/bin/systemctl start autofs.service': Finished command related to promiser '/usr/bin/systemctl start autofs.service' -- succeeded
2013-07-25T18:02:15+0200     info: Completed execution of '/usr/bin/systemctl start autofs.service'

however, only machines having autofs installed with run that check or all of them !? I don't want to pollute all my stations with that check, especially stations that don't run autofs .

Thanks .
Reply all
Reply to author
Forward
0 new messages