[pve-devel] [PATCH] PVE:Daemon start/restart with systemd
Alen Grizonic
a.grizonic at proxmox.com
Wed Jun 17 12:02:31 CEST 2015
Couldn't agree more, so here is the polished version with systemctl
added also to stop the service.
diff --git a/src/PVE/Daemon.pm b/src/PVE/Daemon.pm
index e051500..fb9a923 100644
--- a/src/PVE/Daemon.pm
+++ b/src/PVE/Daemon.pm
@@ -578,6 +578,16 @@ my $read_pid = sub {
return $pid;
};
+# checks if the proces was started by systemd
+my $init_ppid = sub {
+
+ if (getppid() == 1) {
+ return 1;
+ } else {
+ return 0;
+ }
+};
+
sub running {
my ($self) = @_;
@@ -654,7 +664,11 @@ sub register_start_command {
code => sub {
my ($param) = @_;
- $self->start($param->{debug});
+ if (&$init_ppid()) {
+ $self->start($param->{debug});
+ } else {
+ PVE::Tools::run_command(['systemctl', 'start',
$self->{name}]);
+ }
return undef;
}});
@@ -700,8 +714,12 @@ sub register_restart_command {
code => sub {
my ($param) = @_;
- &$reload_daemon($self, $use_hup);
-
+ if (&$init_ppid()) {
+ &$reload_daemon($self, $use_hup);
+ } else {
+ PVE::Tools::run_command(['systemctl', $use_hup ?
'reload-or-restart' : 'restart', $self->{name}]);
+ }
+
return undef;
}});
}
@@ -749,8 +767,12 @@ sub register_stop_command {
code => sub {
my ($param) = @_;
-
- $self->stop();
+
+ if (&$init_ppid()) {
+ $self->stop();
+ } else {
+ PVE::Tools::run_command(['systemctl', 'stop',
$self->{name}]);
+ }
return undef;
}});
On 06/16/2015 04:48 PM, Dietmar Maurer wrote:
> some comments inline
>
>> diff --git a/src/PVE/Daemon.pm b/src/PVE/Daemon.pm
>> index e051500..16e08c9 100644
>> --- a/src/PVE/Daemon.pm
>> @@ -578,6 +578,16 @@ my $read_pid = sub {
>> return $pid;
>> };
>>
>> +my $init_ppid = sub {
>> + my $ppid = getppid();
>> +
>> + if ($ppid == 1) {
>> + return 1;
>> + } else {
>> + return 0;
>> + }
>> +};
>> +
>> sub running {
>> my ($self) = @_;
>>
>> @@ -654,7 +664,11 @@ sub register_start_command {
>> code => sub {
>> my ($param) = @_;
>>
>> - $self->start($param->{debug});
>> + if (&$init_ppid()) {
>> + $self->start($param->{debug});
>> + } else {
>> + PVE::Tools::run_command(['systemctl', 'start',
>> $self->{name}]);
>> + }
>>
>> return undef;
>> }});
>> @@ -666,7 +680,7 @@ my $reload_daemon = sub {
>> if ($self->{env_restart_pve_daemon}) {
>> $self->start();
>> } else {
>> - my ($running, $pid) = $self->running();
>> + my ($running, $pid) = $self->running();
> useless?
>
>> if (!$running) {
>> $self->start();
>> } else {
>> @@ -700,8 +714,23 @@ sub register_restart_command {
>> code => sub {
>> my ($param) = @_;
>>
>> - &$reload_daemon($self, $use_hup);
>> -
>> + if (&$init_ppid()) {
>> + &$reload_daemon($self, $use_hup);
>> + } else {
>> + my ($running, $pid) = $self->running();
>> + if (!$running) {
>> + PVE::Tools::run_command(['systemctl', 'start',
>> $self->{name}]);
>> + } else {
>> + if ($use_hup) {
>> + syslog('info', "send HUP to $pid");
>> + kill 1, $pid;
>> + PVE::Tools::run_command(['systemctl', 'start',
>> $self->{name}]);
> It is already running, so what is the purpose of that 'start'?
>
>> + } else {
>> + PVE::Tools::run_command(['systemctl', 'restart',
>> $self->{name}]);
>> + }
>> + }
>> + }
>> +
> I thought we can simply use the following?
>
> if (&$init_ppid()) {
> &$reload_daemon($self, $use_hup);
> } else {
> PVE::Tools::run_command(['systemctl', $use_hup ? 'reload-or-restart' :
> 'restart', $self->{name}]);
> }
>
> We also want to use systemctl to stop the service.
More information about the pve-devel
mailing list