在systemd中,我需要能够创建脚本,暂存脚本并在下次启动时执行它,一旦执行便将其删除并删除服务。
我已经创建了一些Perl来构建单元文件并暂存将执行和清除其他部分的脚本。由于某种原因,我可以sudo systemctl start onetimerun
进行相关的操作。但是,即使在sudo systemctl enable onetimerun
之后,它仍然无法在启动时启动。
这是我拥有的Perl代码,应该在/ tmp / one-time-run / scripts /
中构建用于运行脚本的相关环境#!/usr/local/bin/perl -w
my $unitFile = <<'END_MESSAGE';
[Unit]
Description=Ooe-time executor for ad-hoc boot scripts
After=basic.target rescue.service rescue.target
[Service]
Type=oneshot
ExecStart=/tmp/one-time-run/runwrangler.pl
[Install]
WantedBy=multi-user.target
END_MESSAGE
my $runScript = <<'END_MESSAGE';
#!/usr/local/bin/perl -w
use Data::Dumper;
my @scriptFiles = map { $_ =~ s/\s//gr } `for i in \$(ls /tmp/one-time-run/scripts/*.sh); do test -x "\$i" -a ! -d "\$i" && echo "\$i"; done;`;
print Dumper @scriptFiles;
foreach my $file (@scriptFiles) {
if(!(system("$file"))) {
print "Failed to run $file\n";
}
else {
system("rm $file");
}
}
system('rm /tmp/one-time-run/runwrangler.pl');
system('rm /run/systemd/system/onetimerun.service');
END_MESSAGE
unless(open UNITFILE, '>/run/systemd/system/onetimerun.service') {
die "\nUnable to create unit file.\n";
}
print UNITFILE $unitFile;
close UNITFILE;
system('mkdir /tmp/one-time-run/');
unless(open WRANGLER, '>/tmp/one-time-run/runwrangler.pl') {
die "\nUnable to create wrangler script.\n";
}
print WRANGLER $runScript;
close WRANGLER;
system('chmod +x /tmp/one-time-run/runwrangler.pl');
system('systemctl daemon-reload');
system('systemctl enable onetimerun');
我希望/ tmp / one-time-run / scripts /中的任何脚本都可以运行,然后在删除单元文件和删除牧马人之前将其删除。
相反,启动时似乎什么也没发生。