diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index ebbc310ddc..8bb5e8b12e 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -793,7 +793,10 @@ func (d *sLinuxRootFs) DeployTelegraf(config string) (bool, error) { return false, errors.Wrap(err, "chmod supervise run script") } initCmd := fmt.Sprintf("%s/supervise %s", cloudMonitorPath, telegrafPath) - err = d.installInitScript("telegraf", initCmd) + if d.isSupportSystemd() { + initCmd = path.Join(telegrafPath, "run") + } + err = d.installInitScript("telegraf", initCmd, false) if err != nil { return false, errors.Wrap(err, "installInitScript") } diff --git a/pkg/hostman/guestfs/fsdriver/linux_init.go b/pkg/hostman/guestfs/fsdriver/linux_init.go index 4b59c18c9f..806b6c782a 100644 --- a/pkg/hostman/guestfs/fsdriver/linux_init.go +++ b/pkg/hostman/guestfs/fsdriver/linux_init.go @@ -31,9 +31,9 @@ func (d *sLinuxRootFs) isSupportSystemd() bool { return d.rootFs.Exists(unitDirPath, false) && d.rootFs.Exists(enableDirPath, false) } -func (d *sLinuxRootFs) installInitScript(name, cmd string) error { +func (d *sLinuxRootFs) installInitScript(name, cmd string, oneshot bool) error { if d.isSupportSystemd() { - return d.installSystemd(name, cmd) + return d.installSystemd(name, cmd, oneshot) } else { return d.installCrond(cmd) } @@ -54,10 +54,15 @@ func (d *sLinuxRootFs) installCrond(cmd string) error { return nil } -func (d *sLinuxRootFs) installSystemd(name, cmd string) error { +func (d *sLinuxRootFs) installSystemd(name, cmd string, oneshot bool) error { var serviceName = fmt.Sprintf("%s.service", name) var unitPath = fmt.Sprintf("%s/%s", unitDirPath, serviceName) var enablePath = fmt.Sprintf("%s/%s", enableDirPath, serviceName) + var serviceType = "simple" + if oneshot { + serviceType = "oneshot" + } + var unitContent = fmt.Sprintf(`[Unit] Description=Run once After=local-fs.target @@ -66,11 +71,11 @@ After=network.target [Service] ExecStart=%s RemainAfterExit=true -Type=oneshot +Type=%s [Install] WantedBy=multi-user.target -`, cmd) +`, cmd, serviceType) err := d.rootFs.FilePutContents(unitPath, unitContent, false, false) if err != nil { return errors.Wrap(err, "save user_data unit fail") diff --git a/pkg/hostman/guestfs/fsdriver/userdata.go b/pkg/hostman/guestfs/fsdriver/userdata.go index 0a90b0268b..1786c18e31 100644 --- a/pkg/hostman/guestfs/fsdriver/userdata.go +++ b/pkg/hostman/guestfs/fsdriver/userdata.go @@ -93,7 +93,7 @@ func (d *sLinuxRootFs) deployUserDataBySystemd(userData string) error { } } { - err := d.installInitScript(serviceName, "/bin/sh "+scriptPath) + err := d.installInitScript(serviceName, "/bin/sh "+scriptPath, true) if err != nil { return errors.Wrap(err, "installInitScript") }