我想在我的机器上自动启动 Apache,但前提是我从 TTY1 登录(我不运行显示管理器)。如果我从任何其他 TTY 登录,它不应该启动。我在我的bash_profile
:
[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && systemctl start httpd
但这是行不通的。其他命令确实像这样工作,所以我假设问题与权限相关(只能su
运行systemctl
)。是否有另一种方法来启动依赖于 TTY 的 httpd 服务器?
我正在运行 Arch(参见这一页例如,X 在没有显示管理器的情况下如何在登录时启动)。
答案1
如果您想为每个 TTY 和每个用户启动一个守护进程,那么有两个简单的解决方案:
不要使用
systemctl
.
在这种情况下,请httpd
直接从您的.xinitrc
shell rc 或您的 shell rc 中运行,如您在帖子中提到的。使用
systemctl --user
。
目前不建议这样做,因为用户会话不是很稳定,也没有官方支持。
坦率地说,我仍在试图弄清楚为什么您要启动像httpd
per-TTY 这样的服务。
答案2
在 Ubuntu 中,有一个脚本 perl (可能是 debian: 的遗产/usr/bin/deb-systemd-invoke
),它可能对您至少可以检查它是否运行以及errorcode
退出时有用。
#!/usr/bin/env perl
# vim:ts=4:sw=4:expandtab
# © 2013 Michael Stapelberg <[email protected]>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of Michael Stapelberg nor the
# names of contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
# .
# THIS SOFTWARE IS PROVIDED BY Michael Stapelberg ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Michael Stapelberg BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=head1 NAME
deb-systemd-invoke - wrapper around systemctl, respecting policy-rc.d
=head1 SYNOPSIS
B<deb-systemd-invoke> start|stop|restart S<I<unit file> ...>
=head1 DESCRIPTION
B<deb-systemd-invoke> is a Debian-specific helper script which asks
/usr/sbin/policy-rc.d before performing a systemctl call.
B<deb-systemd-invoke> is intended to be used from maintscripts to start
systemd unit files. It is specifically NOT intended to be used interactively by
users. Instead, users should run systemd and use systemctl, or not bother about
the systemd enabled state in case they are not running systemd.
=cut
use strict;
use warnings;
if (@ARGV < 2) {
print STDERR "Syntax: $0 <action> <unit file> [<unit file> ...]\n";
exit 1;
}
my $policyhelper = '/usr/sbin/policy-rc.d';
my @units = @ARGV;
my $action = shift @units;
if (-x $policyhelper) {
for my $unit (@units) {
system(qq|$policyhelper $unit "$action"|);
# 104 means run
# 101 means do not run
my $exitcode = ($? >> 8);
if ($exitcode == 101) {
print STDERR "$policyhelper returned 101, not running '" . join(' ', @ARGV) . "'\n";
exit 0;
} elsif ($exitcode != 104) {
print STDERR "deb-systemd-invoke only supports $policyhelper return code 101 and 104!\n";
print STDERR "Got return code $exitcode, ignoring.\n";
}
}
}
exec '/bin/systemctl', @ARGV;