Linux display命令在终端中起作用,但在systemd服务中不起作用
我制作了一个网络应用程序来关闭计算机的屏幕,虽然有几种不同的技术,但是非常简单: 我有一个html / js前端,它检测按钮单击(屏幕打开/屏幕关闭),该按钮通过ajax将选项发送到PHP后端 然后,PHP通过TCP端口连接,将选项发送到用golang编写的程序 然后,我的golang程序执行命令以关闭/打开屏幕。 它运行的命令是(“ xset -display:0 dpms force off”) 我遇到的问题是该命令仅在在终端中运行golang程序时才有效,但是当我将其设置为服务时,该命令将不起作用。 这是golang代码:
package main

import (
    "os/exec"
    "net"
    "fmt"
    "bufio"
)

func main() {
    fmt.Println("Launching server")

    ln, _ := net.Listen("tcp", ":7777")
    fmt.Println("Listening...
")

    for {
        // accept connection on port
        conn, _ := ln.Accept()
        fmt.Println("New connection")

        // listen for message ending in 

        message, _ := bufio.NewReader(conn).ReadString('
')
        rec := string(message)

        // remove trailing 

        rec = rec[:len(rec)-1]

        fmt.Println("Message Received: ", "\""+rec+"\"")

        returnMessage := "fail"

        if (rec == "screensOff") {
            fmt.Println("Turning off screens...")

            //execute screens off command
            cmd := exec.Command("xset", "-display", ":0", "dpms", "force", "off")
            stdout, err := cmd.Output()

            if err != nil {
                fmt.Println(err.Error())
            } else {
                fmt.Println(string(stdout))
                returnMessage = "done"
            }
        } else if (rec == "screensOn") {
            fmt.Println("Turning on screens...");

            //execute screens on command
            cmd := exec.Command("xset", "-display", ":0", "dpms", "force", "on")

            stdout, err := cmd.Output()
            if err != nil {
                fmt.Println(err.Error())
            } else {
                fmt.Println(string(stdout))
                returnMessage = "done"
            }
            returnMessage = "done"
        } 

        conn.Write([]byte(returnMessage + "
"))

        conn.Close()
        fmt.Println("Connection closed
")
    }
}

相关的PHP代码:

<?php
function sendServiceMessage($message) {
    $host = "localhost";
    $port = 7777;
    $timeout = 30;

    // connect to service
    $socket = fsockopen($host, $port, $errnum, $errstr, $timeout);
    if (!is_resource($socket)) {
        exit("connection fail: ".$errnum." ".$errstr);
    }
    else {
        // send message
        fputs($socket, $message."
");

        // receive return message
        $recieved = "";
        while (!feof($socket)) {
            $recieved .= fgets ($socket, 1024);
        }
    }

    // close connection
    fclose($socket);
    if ($recieved == "done") {
        return true;
    }
    return false;   
}

sendServiceMessage("screensOff");

我使用systemd来设置服务,因此在构建程序并将其放在/usr/bin/中之后/

...$ go build screenControl.go
...$ sudo cp screenControl /usr/bin/screenControl

I can run the screenControl program in the terminal, and select "screens off" in the web app and it all works as expected:

...$ screenControl
Launching server
Listening...

New Connection
Message Received:  "screensOff"
Turning off screens...

Connection closed

然后,我创建了一个systemd单位文件(/etc/systemd/system/screenControl.service):

[Unit]
Description=Screen control service

[Service]
ExecStart=/usr/bin/screenControl
Restart=on-abort

[Install]
WantedBy=multi-user.target

于是我启动服务并检查了一下它:

...$ systemctl start screenControl
...$ systemctl status screenControl
● screenControl.service - Screen control service
   Loaded: loaded (/etc/systemd/system/screenControl.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2015-12-13 22:31:54 GMT; 6s ago
 Main PID: 19871 (screenControl)
   CGroup: /system.slice/screenControl.service
           └─19871 /usr/bin/screenControl

Dec 13 22:31:54 User systemd[1]: Started Screen control service.
Dec 13 22:31:54 User screenControl[19871]: Launching server
Dec 13 22:31:54 User screenControl[19871]: Listening...

因此它正在运行,但是当我现在在Web应用程序中选择关闭屏幕时,什么也没发生...我再次检查了服务状态,它收到了关闭屏幕的消息,但是命令退出并出现错误:

...
Dec 13 22:31:54 User screenControlTest[19871]: Launching server
Dec 13 22:31:54 User screenControlTest[19871]: Listening...
Dec 13 22:32:25 User screenControlTest[19871]: New connection
Dec 13 22:32:25 User screenControlTest[19871]: Message Received:  "screensOff"
Dec 13 22:32:25 User screenControlTest[19871]: Turning off screens...
Dec 13 22:32:25 User screenControlTest[19871]: exit status 1
Dec 13 22:32:25 User screenControlTest[19871]: Connection closed

这是什么问题,如何使该命令作为服务工作? 一旦运行正常,我想在机器开机时自动启动服务,尽管使用systemd我认为这很简单:

...$ systemctl enable screenControl
任何帮助将是伟大的,谢谢:) 编辑: 在让golang程序显示xset命令的stderr之后,我现在也有了错误消息:
xset:  unable to open display ""
dongtang4954
dongtang4954 真棒!我使用以下命令设置显示环境:cmd.Env=[]string{“DISPLAY=:0”};谢谢:)
4 年多之前 回复
dsgd5756
dsgd5756 当显示“”时,表示它不知道哪个显示。当您从头开始执行命令行应用程序时,可以通过环境,尝试使用DISPLAY=“:0”进行操作
4 年多之前 回复
dongwuwan5646
dongwuwan5646 我玩过go并得到它也向我显示了stderr输出:(退出状态1:xset:无法打开显示“”)我想知道这是否像您说的那样,并且该root被阻塞了,尽管我可以从根终端运行golang程序,它可以工作..但这是可行的,因为我从桌面用户会话中启动了根会话(通过“sudo-i”)?这比我预期的要深入,有趣的东西:)
4 年多之前 回复
dongqiang8683
dongqiang8683 您是否生成了与退出代码1一起生成的实际错误消息?
4 年多之前 回复
dongzhang8475
dongzhang8475 我停止了该服务,尝试了“xhost+”,它表示“访问控制已禁用,客户端可以从任何主机进行连接”,然后再次启动了该服务,但是它仍然从xset命令中获得“退出状态1”:/
4 年多之前 回复
dou1908
dou1908 xwindows不允许当前会话所有者以外的用户向其发送命令。在调用服务之前,请尝试从外壳程序(如您)中使用“xhost+”。可能是根已被阻止关闭屏幕。但是,由于您拥有会话,因此在您从外壳运行命令时可以使用
4 年多之前 回复

2个回答

As per David Budworth's comment, the fix was exceedingly simple; as the service was running under root, it didn't have the DISPLAY environment variable set.

In go you can set the environment variables when using exec like so:

//execute screens off command
cmd := exec.Command("xset", "-display", ":0", "dpms", "force", "off")
cmd.Env = []string{"DISPLAY=:0"} // set the display before executing
stdout, stderr := cmd.CombinedOutput() //execute and return all output

And from James Henstridge answer I found I also needed to run xhost +SI:localuser:root to allow the root user access to the X server.

You can do this for users after they've logged in by adding this line to the top of the /etc/profile file

xhost +SI:localuser:root > /dev/null 2>&1

OR

You can get it to work even when no user is logged in (when the login screen is showing)

First I created the directory /opt/scripts then created the file /opt/scripts/xhost.sh and gave it executable permissions with chmod +x /opt/scripts/xhost.sh

In this file is just the one line:

xhost +SI:localuser:root > /dev/null 2>&1

Then edit the file /etc/lightdm/lightdm.conf (I had to create it, but edit it if it's there) and add the line display-setup-script=/opt/scripts/xhost.sh

So my lightdm.conf file looks like this:

[SeatDefaults]
greeter-session=unity-greeter
user-session=ubuntu
display-setup-script=/opt/scripts/xhost.sh

This tells LightDM (the display manager running in Ubuntu) to run the script /opt/scripts/xhost.sh after the X server is started but before anything else, therefore root is given the xhost authorization straightaway!

note:

display-setup-script is run after the X server starts but before the user session / greeter is run. Set this if you need to configure anything special in the X server. It is run as root. If this command returns an error code the X server is stopped. Source: https://wiki.ubuntu.com/LightDM



xset </ code>命令只是X服务器的客户端。 它通过检查 DISPLAY </ code>环境变量来确定与哪个X服务器通信,当您将命令作为系统服务运行时将不会设置该环境变量。</ p>

即使您确保在运行守护程序时设置了 DISPLAY </ code>,它也可能会以其他用户帐户运行,并且默认情况下会拒绝访问显示。</ p>

A 更好的选择是将守护程序作为用户会话的一部分运行。 这将解决身份验证问题(它将按您的方式运行)以及定位显示的能力(环境变量应可见)。 当您未登录时,守护程序将不会运行,但这对于特定的用例可能并不重要。</ p>

您已用“ Ubuntu”标记了您的问题, 该会话仍由 Upstart 管理。 您可以通过在〜/ .config / upstart </ code>中创建文件来创建新的用户会话作业。 可以在 init(5 )</ code>手册页。</ p>
</ div>

展开原文

原文

The xset command is just a client for the X server. It determines which X server to talk to by checking the DISPLAY environment variable, which won't be set when you run your command as a system service.

And even if you ensured DISPLAY was set when running your daemon, it would likely be run as a different user account and denied access to the display by default.

A better option would be to run the your daemon as part of the user session. This will solve the authentication issues (it will be running as you), and the ability to locate the display (the environment variable should be visible). The daemon won't be running when you aren't logged in, but that probably doesn't matter for this particular use case.

You've tagged your question with "Ubuntu", where the session is still managed by Upstart. You can create new user session jobs by creating a file in ~/.config/upstart. The details of the file format can be found in the init(5) man page.

dongyilai4214
dongyilai4214 在15.04之前,您将运行Upstart的两个副本:系统一个作为pid 1,第二个作为您的用户帐户运行以管理桌面登录会话。 该系统实例确实已被systemd取代,但用户会话1仍然存在。 在某个时候它也可能会被systemd取代,但这还没有发生。
4 年多之前 回复
dsx5201
dsx5201 通过将xhost + SI:localuser:root放在/ etc / profile文件的顶部而不是bash.bashrc文件来解决。 谢谢您的帮助 :)
4 年多之前 回复
douba5540
douba5540 现在,它仅在登录到桌面后才可以工作(很好,如果它在登录屏幕上可以工作,那很好,但是,嗯),然后必须打开终端窗口...:s,但关闭后仍然可以工作 终端...我什至不知道要用谷歌来解决那个问题:s如此接近工作!
4 年多之前 回复
doufubu2518
doufubu2518 我注意到我可以运行xhost + SI:localuser:root来授权该root用户,因此我将其放在/etc/bash.bashrc文件的底部以为所有用户运行,希望这没有什么不对: )
4 年多之前 回复
dtpt75860
dtpt75860 我以为Upstart在15.04中被systemd取代了? (我正在15.10上运行)但是,实际上,除非我从用户会话中运行“ xhost +”,否则它将无法工作...是否可以授权root用户也可以访问显示内容? 因此,无论谁登录,它仍然可以使用。 这样做会有任何问题吗?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
运行二进制作为systemd服务不起作用

<div class="post-text" itemprop="text"> <p>I have a go project and when I compile it I get your typical binary. If I call the binary directly from the command line with the adequate inputs, it runs perfectly well. I want to make this program part of the systemd ecosystem and the following is my my <code>gobinary.service</code></p> <pre><code>[Unit] Description=Run Go Service [Service] WorkingDirectory=/path/to/directory/ ExecStart=/path/to/directory/binary --config config/service.conf Restart=always [Install] WantedBy=multi-user.target </code></pre> <p>I can start and stop the service using the usual <code>sudo service gobinary start</code> without error messages but the problem is that the program is actually not running because it does not respond to the inputs, namely it has a rabbitmq input queue, despite me sending content to the queue it doesn't react. If however I run the program using the binary directly <code>$ ./binary --config/service.conf</code> everything runs smoothly. How do I troubleshoot this?</p> </div>

如何在systemd服务中正确设置php脚本?

<div class="post-text" itemprop="text"> <p>SO, I'm trying to set up a service using systemd on an Axis camera (I believe it's Arch linux or a very stripped down Linux version that's custom). I have the service set up as follows:</p> <p>myservice.service:</p> <pre><code>[Unit] Description=Ping Monitoring Daemon After=network.target After=network-online.target [Service] Type=oneshot ExecStart=/etc/init.d/listen-start.sh ExecStop=/etc/init.d/listen-stop.sh RemainAfterExit=yes [Install] WantedBy=multi-user.target </code></pre> <p>Here is listen-start.sh:</p> <pre><code>#!/bin/sh echo $(curl http://website.com/logssomethingtoadatabase.php?action=start) </code></pre> <p>My php script:</p> <pre><code>&lt;?php echo 'Service Started'; while (true) { $foo = print_r($_SERVER, true); $blnStop = false; if ( $_GET['action'] === 'start') { mail("test@myemail.com","Debug", "Camera service started with current values: " . $foo); } else { mail("test@myemail.com","Debug", "Camera service stopped with current values: " . $foo); $blnStop = true; } if ( $blnStop ) { break 2; } sleep(60); } echo 'Service Stopped'; die(); ?&gt; </code></pre> <p>Issue I'm encountering is once I start the service, I'm unable to "stop it", even after issuing the "systemctl stop" and "disable" commands on it. I have to power cycle the camera to get it to stop emailing me. I can't seem to get it to "break" out of the while loop.</p> <p>The goal here is to call that script every 60 seconds which is a device monitoring effort. I must use systemd in this case as that's all that the camera supports.</p> <p>Additional info: The systemctl start and stop commands work successfully, and the script itself runs fine.</p> <p>Can anyone see anything I may be missing?</p> </div>

systemd在docker容器中

<div class="post-text" itemprop="text"> <p>I have a server with ubuntu 14.04 (that does not use systemd), I use HHVM but I want to use php 7 now. I was thinking, why not use a container with php7.0-fpm, this should work.</p> <p>I first tried with a centos 7 (that uses systemd) container, I could install everything, but the problem is when I try to start the service, I get this: "Failed to get D-Bus connection: Operation not permitted"</p> <p>In the end, I used an Ubuntu 14.04 container where I installed php7 with fpm and it worked, but will I get this problem every time I want to use am container that uses systemd from a system that doesn't? (and what about the opposite)</p> <p>Thank you :)</p> </div>

Linux systemd 自启动脚本无法正常运行(求助分析方向)

在RHEL8.0上,通过systemd service用以下脚本来实现登录后自动切换壁纸。 服务为test.service, bash脚本为test.sh 单独运行Set_background.sh 没有问题,但是当通过test.service去调用的时候,只能设置一次。 设置壁纸的命令成功,但是重置的命令失败。 考虑是因为通过test.service去调用test.sh时,线程出现问题。 请高手指教从哪里去分析。 **test.service内容如下:** ``` [Unit] Description=Change Desktop background [Service] ExecStart=/home/test/Desktop/Set_background.sh After=gdm.service [Install] WantedBy=graphical.target ``` **Set_background.sh内容如下:** ``` #!/bin/bash while true do /usr/bin/gsettings set org.gnome.desktop.background picture-uri 'file:///home/test/Desktop/8-dark.xml' sleep 3 /usr/bin/gsettings reset org.gnome.desktop.background picture-uri sleep 3 done ```

如何注册systemd服务生命周期事件?

<div class="post-text" itemprop="text"> <p>I want to be informed about all state changes of service units. I know there is the DBUS API which gives me signals for UnitNew and UnitRemoved, but the semantic is still unclear to me. Also the signals doesn't provide the informations I need.</p> <p>I'm using go and tried the <a href="https://github.com/coreos/go-systemd" rel="nofollow noreferrer">go-systemd lib</a> from coreos. They provide a way to "subscribe" but it's in fact polling all the unit information available in the system. I do not want to do so every few milliseconds, just to get informed about statechanges.</p> <p>I also tried the default org.freedesktop.DBus.Properties but I can not get this working.</p> <p>Please help If you can, I'm out of ideas.</p> </div>

作为systemd服务运行时服务器内存不足

<div class="post-text" itemprop="text"> <p>I have created a webserver using the Echo router. It act as a REST endpoint. I tried to deploy it in an Ubuntu server using <code>systemd</code>. The requests are getting out of memory when I try to run it as a service. However it is working fine when I run it in terminal What could be the issue.</p> <p><strong>my service file</strong></p> <pre><code>[Unit] Description=myapp [Service] Type=simple User=root Environment=ENV=PRODUCTION ExecStart=/var/www/myapp/public_html/my.app Restart=always RestartSec=5s [Install] WantedBy=multi-user.target </code></pre> </div>

linux下的bin和/usr/bin目录下的命令有什么不同?

各位大牛,请问linux下的bin和/usr/bin目录下的命令有什么不同? 另外,我看视频时有说到,bin下的命令可以在单用户模式下执行,那请问是所有bin下的命令都可以吗?

从systemd启动主进程时无法分离子进程

<div class="post-text" itemprop="text"> <p>I want to spawn long-running child processes that survive when the main process restarts/dies. This works fine when running from the terminal:</p> <pre><code>$ cat exectest.go package main import ( "log" "os" "os/exec" "syscall" "time" ) func main() { if len(os.Args) == 2 &amp;&amp; os.Args[1] == "child" { for { time.Sleep(time.Second) } } else { cmd := exec.Command(os.Args[0], "child") cmd.SysProcAttr = &amp;syscall.SysProcAttr{Setsid: true} log.Printf("child exited: %v", cmd.Run()) } } $ go build $ ./exectest ^Z [1]+ Stopped ./exectest $ bg [1]+ ./exectest &amp; $ ps -ef | grep exectest | grep -v grep | grep -v vim snowm 7914 5650 0 23:44 pts/7 00:00:00 ./exectest snowm 7916 7914 0 23:44 ? 00:00:00 ./exectest child $ kill -INT 7914 # kill parent process [1]+ Exit 2 ./exectest $ ps -ef | grep exectest | grep -v grep | grep -v vim snowm 7916 1 0 23:44 ? 00:00:00 ./exectest child </code></pre> <p>Note that the child process is still alive after parent process was killed. However, if I start the main process from systemd like this...</p> <pre><code>[snowm@localhost exectest]$ cat /etc/systemd/system/exectest.service [Unit] Description=ExecTest [Service] Type=simple ExecStart=/home/snowm/src/exectest/exectest User=snowm [Install] WantedBy=multi-user.target $ sudo systemctl enable exectest ln -s '/etc/systemd/system/exectest.service' '/etc/systemd/system/multi-user.target.wants/exectest.service' $ sudo systemctl start exectest </code></pre> <p>... then the child also dies when I kill the main process:</p> <pre><code>$ ps -ef | grep exectest | grep -v grep | grep -v vim snowm 8132 1 0 23:55 ? 00:00:00 /home/snowm/src/exectest/exectest snowm 8134 8132 0 23:55 ? 00:00:00 /home/snowm/src/exectest/exectest child $ kill -INT 8132 $ ps -ef | grep exectest | grep -v grep | grep -v vim $ </code></pre> <p>How can I make the child survive?</p> <p>Running go version go1.4.2 linux/amd64 under CentOS Linux release 7.1.1503 (Core).</p> </div>

Linux开机启动服务顺序问题

有这么一个问题,我将tomcat的启动操作添加到Linux的开机启动服务项中了,但是却在数据库连接127.0.0.1的时候出现无效的地址,web应用无法启动。但是不通过开机启动,手动启动tomcate的话,web应用就能正常启动。这是什么原因导致的呢?

尝试在systemd下运行可访问X服务器的服务

<div class="post-text" itemprop="text"> <p>I'm using manjaro linux (arch based) with systemd. I want to make a daemon service that runs in the background and takes screenshots of my screen at random intervals, up to 10 minutes apart.</p> <p>I'm using Go, and I have a program that works just fine if I build it and run it from the command line (see below). I've made a .service file (see below) for the service and I've started it with <code>systemctl start screenshot</code> and confirmed it's running with <code>systemctl is-active screenshot</code>. However, it doesn't work. No screenshots are being saved like if I run it from the command line.</p> <p>Out of interest I tried running it with <code>nohup ./screenshot &amp;</code> and it suffers from the same problem as my daemon attempt.</p> <p>Is there some sort of problem with using the date package in the background? Or could it perhaps be the screenshot library?</p> <p><code>screenshot.go</code> </p> <pre><code>package main import ( "fmt" "image/png" "math/rand" "os" "time" "github.com/coreos/go-systemd/daemon" "github.com/vova616/screenshot" ) func main() { daemon.SdNotify(false, "READY=1") i := 0 for { r := rand.Intn(10) time.Sleep(time.Duration(r) * time.Minute) t := time.Now() year, month, day := t.Date() date := fmt.Sprintf("%d-%s-%d", year, month.String(), day) img, err := screenshot.CaptureScreen() if err != nil { fmt.Println(err) continue } hour, min, sec := t.Clock() stamp := fmt.Sprintf("%d:%d:%d", hour, min, sec) fmt.Printf("Taking screenshot %s ", stamp) f, err := os.Create(fmt.Sprintf("/home/dave/screenshot/%s/%s.png", date, stamp)) if err != nil { fmt.Println(err) continue } err = png.Encode(f, img) if err != nil { fmt.Println(err) continue } err = f.Close() if err != nil { fmt.Println(err) continue } i++ daemon.SdNotify(false, "WATCHDOG=1") } } </code></pre> <p>and here is the service file</p> <pre><code>[Unit] Description=Random Screenshot Service [Service] Type=notify ExecStart=/home/dave/screenshot/screenshot WatchdogSec=30s Restart=on-failure [Install] WantedBy=multi-user.target </code></pre> </div>

为什么systemd无法启动golang Web应用程序,没有答案

<div class="post-text" itemprop="text"> <p>My golang web app cannot start when I use systemd, but it works fine when manually start. What are problems with my systemd configuration?</p> <p><strong>goweb.service</strong></p> <pre><code>$ cat goweb.service [Unit] Description=Backend service After=network.target [Service] User=myapp Group=myapp Restart=on-failure ExecStart=/u01/backend [Install] WantedBy=multi-user.target </code></pre> <p><code>backend</code> is a binary file compiled with command: <code>env GOOS=linux GOARCH=amd64 go build -v bitbucket.org/myapp/backend</code></p> <p><strong>systemd service status</strong></p> <pre><code>$ sudo service goweb status Redirecting to /bin/systemctl status goweb.service ● goweb.service - Backend service Loaded: loaded (/usr/lib/systemd/system/goweb.service; disabled; vendor preset: disabled) Active: inactive (dead) May 18 10:55:56 instance-1 systemd[1]: Started Backend service. May 18 10:55:56 instance-1 systemd[1]: Starting Backend service... </code></pre> <p><strong>P/S</strong>: It looks like my web app started, but then stoped immediately.</p> <p>I try config <code>Type=forking</code>, then service status show as below. Could someone explains why the log <code>Started Backend service.</code> and <code>Starting Backend service...</code> order is reversed.</p> <pre><code>$ sudo service goweb status Redirecting to /bin/systemctl status goweb.service ● goweb.service - Backend service Loaded: loaded (/usr/lib/systemd/system/goweb.service; enabled; vendor preset: disabled) Active: inactive (dead) since Wed 2016-05-18 11:06:02 UTC; 2s ago Process: 25847 ExecStart=/u01/backend (code=exited, status=0/SUCCESS) May 18 11:06:02 instance-1 systemd[1]: Starting Backend service... May 18 11:06:02 instance-1 systemd[1]: Started Backend service. </code></pre> <p>Result when I run web app manually (from terminal):</p> <pre><code>$ /u01/backend [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] POST /upload --&gt; main.uploadFileHandler (3 handlers) [GIN-debug] Environment variable PORT="9005" [GIN-debug] Listening and serving HTTP on :9005 </code></pre> <hr> <p><strong>Update</strong>:</p> <ol> <li>I try <a href="http://supervisord.org/" rel="nofollow">supervisord</a> and it works fine (same binary file)</li> <li>With <code>systemd</code>, I changed service config to <code>Restart=always</code>, <code>RestartSec=15</code>. And <code>systemd</code> keeps restarting my web app.</li> </ol> <p>Does anyone know why <code>supervisord</code> work fine but <code>systemd</code> not? I think that <code>systemd</code> would work fine with a basic task like that!</p> </div>

使用Golang管理Linux守护程序

<div class="post-text" itemprop="text"> <p>I need to manage the varnish service through an application in Golang, more specifically I need to have the application change the port, ip, storage method and etc. At first I started by following the official varnish documentation (<a href="https://varnish-cache.org/docs/trunk/tutorial/putting_varnish_on_port_80.html" rel="nofollow noreferrer">https://varnish-cache.org/docs/trunk/tutorial/putting_varnish_on_port_80.html</a>), so I did with that the application created the following file in the path: /etc/systemd/system/varnish.service.d/customexec.conf</p> <blockquote> <pre><code>[Service] ExecStart= ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret </code></pre> </blockquote> <p>There was no error, however, the service does not start In the service status it displays the following message:</p> <pre><code>● varnish.service - Varnish HTTP accelerator Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled) Drop-In: /etc/systemd/system/varnish.service.d └─customexec.conf Active: inactive (dead) since Dom 2018-01-28 15:10:46 -03; 3h 56min ago Docs: https://www.varnish-cache.org/docs/4.1/ man:varnishd Process: 970 ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret (code=exited, status=0/SUCCESS) Main PID: 970 (code=exited, status=0/SUCCESS) </code></pre> <p>Is there another way to manage a service across an application?</p> <p>Thank you all</p> </div>

liunx 安装 systemd-219-42.el7.x86_64.rpm

为啥总是有那么多要安装啊!求大神指点 rpm -ivh systemd-219-42.el7.x86_64.rpm warning: systemd-219-42.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY error: Failed dependencies: kmod >= 18-4 is needed by systemd-219-42.el7.x86_64 libc.so.6(GLIBC_2.16)(64bit) is needed by systemd-219-42.el7.x86_64 libc.so.6(GLIBC_2.17)(64bit) is needed by systemd-219-42.el7.x86_64 libcryptsetup.so.4()(64bit) is needed by systemd-219-42.el7.x86_64 libcryptsetup.so.4(CRYPTSETUP_1.0)(64bit) is needed by systemd-219-42.el7.x86_64 libkmod.so.2()(64bit) is needed by systemd-219-42.el7.x86_64 libkmod.so.2(LIBKMOD_5)(64bit) is needed by systemd-219-42.el7.x86_64 liblzma.so.5()(64bit) is needed by systemd-219-42.el7.x86_64 liblzma.so.5(XZ_5.0)(64bit) is needed by systemd-219-42.el7.x86_64 libmount.so.1(MOUNT_2.20)(64bit) is needed by systemd-219-42.el7.x86_64 libqrencode.so.3()(64bit) is needed by systemd-219-42.el7.x86_64 systemd-libs = 219-42.el7 is needed by systemd-219-42.el7.x86_64 dracut < 033-499 conflicts with systemd-219-42.el7.x86_64 initscripts < 9.49.28-1 conflicts with systemd-219-42.el7.x86_64

CentOS7 systemd管理mysql的一个问题

基础环境版本: CentOS7.3 原生mysql5.7 我是通过下载官网的rpm包进行安装mysql-server的, 问题现象: 通过使用官网下载的rpm安装好mysql-server后,在配置文件中配置好mysql服务端 ![图片说明](https://img-ask.csdn.net/upload/201809/25/1537865812_725068.jpg) 创建好相关目录,配置好权限后,使用systemct启动应用 但是当我查看启动的mysql进程时,突然发现pid-file是/var/run/mysqld/mysqld.pid,但是我配置中写的是/opt/mysql/3306/tmp/mysqld.pid![图片说明](https://img-ask.csdn.net/upload/201809/25/1537866258_197098.jpg) 查看了一下,发现/usr/lib/systemd/system/mysqld.service(mysql的systemd注册服务文件)中已经把mysql的pid文件名写死了![图片说明](https://img-ask.csdn.net/upload/201809/25/1537866276_212217.jpg) 再次尝试修改pid文件路径,改为/opt/mysql/3306/tmp/mysqld.pid![图片说明](https://img-ask.csdn.net/upload/201809/25/1537866343_369532.jpg) 使用systemctl daemon-reload重新加载mysqd.service文件 再次使用systemctl启动mysqld,但是这时,mysqld进程启动正常,pid文件也写入了正确的路径中,但是systemctl一直没有返回,另起一个section,用systemctl status mysqld查询状态,发现mysqld一直处于activating (start)状态![图片说明](https://img-ask.csdn.net/upload/201809/25/1537866644_455249.jpg) 这时mysql-server可以正常使用,systemctl start mysqld的进程强制停掉也不会影响正常使用,但mysqld的服务状态也不会变为正常的active (running) 兄弟们有遇到过类似的状况么??求解一下,这个是什么原理?

Systemd无法检测到GOPATH(在没有二进制文件的情况下运行)

<div class="post-text" itemprop="text"> <p>I wrote simple systemd service-file <code>goserver.service</code>:</p> <pre><code>[Unit] Description=Goserver [Service] ExecStart=/usr/bin/go run /home/denis/goserver/index.go [Install] WantedBy=multi-user.target </code></pre> <p>But when I try to run service and get status:</p> <pre><code>$ sudo systemctl status goserver.service </code></pre> <p>I get error:</p> <pre><code>мар 02 12:28:04 DV go[13627]: home/denis/goserver/index.go:6:2: cannot find package "gopkg.in/mgo.v2" in any of: мар 02 12:28:04 DV go[13627]: /usr/lib/go-1.6/src/gopkg.in/mgo.v2 (from $GOROOT) мар 02 12:28:04 DV go[13627]: ($GOPATH not set) </code></pre> <p>But <code>$GOPATH</code> is set with <code>export</code> command:</p> <pre><code>$ export GOPATH="/home/denis/goserver/" $ ls $GOPATH goserver.log index.go pkg src templates </code></pre> <p>And this command from command-line works fine:</p> <pre><code>$ /usr/bin/go run /home/denis/goserver/index.go </code></pre> <p>But if I use systemd service it doesn't work.</p> <p>How can I run systemd service with detected <code>$GOPATH</code>? (without binary file created by command <code>go build...</code>)</p> </div>

linux中当用户在登录时遇到无法登录的问题,或出现一些预期之外的错误时,会将数据记录在哪里

如题当用户在登录时遇到无法登录的问题,或出现一些预期之外的错误时,会将数据记录在 什么地方,可用于让系统管理员排查问题

linux 自定义jar 注册到服务中

​ [root@centos administrator]# systemctl status quote -l ● quote.service Loaded: loaded (/etc/systemd/system/quote.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since 五 2018-08-31 16:16:59 CST; 25s ago Process: 33508 ExecStart=/usr/lib/jvm/jdk1.8.0_181/bin/java -jar /work/quote.jar (code=exited, status=143) 8月 31 16:16:17 centos java[33508]: 2018-08-31 16:16:17.224 [http-nio-8089-exec-1] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started 8月 31 16:16:17 centos java[33508]: 2018-08-31 16:16:17.236 [http-nio-8089-exec-1] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 12 ms 8月 31 16:16:59 centos systemd[1]: quote.service start operation timed out. Terminating. 8月 31 16:16:59 centos java[33508]: 2018-08-31 16:16:59.462 [Thread-4] INFO o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7ab2bfe1: startup date [Fri Aug 31 16:16:05 CST 2018]; root of context hierarchy 8月 31 16:16:59 centos java[33508]: 2018-08-31 16:16:59.463 [Thread-4] INFO o.s.jmx.export.annotation.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown 8月 31 16:16:59 centos java[33508]: 2018-08-31 16:16:59.464 [Thread-4] INFO o.s.jmx.export.annotation.AnnotationMBeanExporter - Unregistering JMX-exposed beans 8月 31 16:16:59 centos systemd[1]: quote.service: control process exited, code=exited status=143 8月 31 16:16:59 centos systemd[1]: Failed to start quote.service. 8月 31 16:16:59 centos systemd[1]: Unit quote.service entered failed state. 8月 31 16:16:59 centos systemd[1]: quote.service failed. ​ ``` ``` #!/ bin / sh [unit] [服务] Type = forking ExecStart = / usr / lib / jvm / jdk1.8.0_181 / bin / java -jar /work/quote.jar ExecStop = / bin / kill -15 $ MAINPID SuccessExutStatus = 200 KillMode =进程 TimeoutSet = infinty [Install] WantedBy = multi-user.target jar包会启动执行,也可以在地址栏上访问,过了一会服务就直接断开了,求教

linux用root给普通用户授权

#创建一个新用户suyan 赋予以下权限 麻烦CSDN的大神们帮忙 1.根目录、dcos目录下面文件有读写权限 2.systemctl命令可以使用包括systemctl start 、systemctl stop、systemctl restart 、systemctl status 3./user/local/bin下面的文件有读写、使用权限 3.可以使用chmod命令groupadd、usermod等命令 4.可以使用yum命令并配置yum源,通过yum命令安装程序。 5.可以是用网络相关的telnet、iptables、route、tcpdump、ssh等命令 6.可以使用docker命令 7.可以使用磁盘相关命令lsblk、fdisk等命令 8.reboot命令

求助Centos 7 开机不执行rc.local 服务

本人刚开始接触Centos系统,需要设置开机启动一个QT编译生成的程序。在CSDN中看到很多类似的方法,但是都解决不了我的问题。 目前已经将编写的脚本文件通过/etc/rc.d/rc.local来执行: #!/bin/bash touch /var/lock/subsys/local; cd /home/ie/desktop/test ./test.sh & exit 0 目前在终端中执行./rc.local可以正确执行/home/ie/desktop/test中的test.sh 但是对应的/usr/lib/systemd/system/rc-local.service总是处于以下状态: [root@localhost rc.d]# systemctl status rc-local.service ● rc-local.service - /etc/rc.d/rc.local Compatibility Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled) Active: active (exited) (Result: exit-code) since Thu 2019-02-14 21:02:25 CST; 1h 58min ago Main PID: 42953 (code=exited, status=1/FAILURE) Tasks: 0 CGroup: /system.slice/rc-local.service Feb 14 21:02:25 localhost.localdomain systemd[1]: Starting /etc/rc.d/rc.local Compatibility… Feb 14 21:02:25 localhost.localdomain systemd[1]: Started /etc/rc.d/rc.local Compatibility. Feb 14 21:02:25 localhost.localdomain rc.local[42950]: QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to ‘/tmp/runtime-root’ Feb 14 21:02:25 localhost.localdomain rc.local[42950]: qt.qpa.screen: QXcbConnection: Could not connect to display Feb 14 21:02:25 localhost.localdomain rc.local[42950]: Could not connect to any X display. Feb 14 21:02:25 localhost.localdomain systemd[1]: rc-local.service: main process exited, code=exited, status=1/FAILURE 望曾遇到类似问题的朋友来帮忙解决!

C/C++学习指南全套教程

C/C++学习的全套教程,从基本语法,基本原理,到界面开发、网络开发、Linux开发、安全算法,应用尽用。由毕业于清华大学的业内人士执课,为C/C++编程爱好者的教程。

定量遥感中文版 梁顺林著 范闻捷译

这是梁顺林的定量遥感的中文版,由范闻捷等翻译的,是电子版PDF,解决了大家看英文费时费事的问题,希望大家下载看看,一定会有帮助的

YOLOv3目标检测实战:训练自己的数据集

YOLOv3是一种基于深度学习的端到端实时目标检测方法,以速度快见长。本课程将手把手地教大家使用labelImg标注和使用YOLOv3训练自己的数据集。课程分为三个小项目:足球目标检测(单目标检测)、梅西目标检测(单目标检测)、足球和梅西同时目标检测(两目标检测)。 本课程的YOLOv3使用Darknet,在Ubuntu系统上做项目演示。包括:安装Darknet、给自己的数据集打标签、整理自己的数据集、修改配置文件、训练自己的数据集、测试训练出的网络模型、性能统计(mAP计算和画出PR曲线)和先验框聚类。 Darknet是使用C语言实现的轻型开源深度学习框架,依赖少,可移植性好,值得深入探究。 除本课程《YOLOv3目标检测实战:训练自己的数据集》外,本人推出了有关YOLOv3目标检测的系列课程,请持续关注该系列的其它课程视频,包括: 《YOLOv3目标检测实战:交通标志识别》 《YOLOv3目标检测:原理与源码解析》 《YOLOv3目标检测:网络模型改进方法》 敬请关注并选择学习!

sql语句 异常 Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your

在我们开发的工程中,有时候会报 [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ------ 这种异常 不用多想,肯定是我们的sql语句出现问题,下面...

浪潮集团 往年的软件类 笔试题 比较详细的哦

浪潮集团 往年的软件类 笔试题 比较详细的哦

2019 AI开发者大会

2019 AI开发者大会(AI ProCon 2019)是由中国IT社区CSDN主办的AI技术与产业年度盛会。多年经验淬炼,如今蓄势待发:2019年9月6-7日,大会将有近百位中美顶尖AI专家、知名企业代表以及千余名AI开发者齐聚北京,进行技术解读和产业论证。我们不空谈口号,只谈技术,诚挚邀请AI业内人士一起共铸人工智能新篇章!

I2c串口通信实现加速度传感器和FPGA的交流

此代码能实现加速度传感器与FPGA之间的交流,从而测出运动物体的加速度。

Python可以这样学(第一季:Python内功修炼)

董付国系列教材《Python程序设计基础》、《Python程序设计(第2版)》、《Python可以这样学》配套视频,讲解Python 3.5.x和3.6.x语法、内置对象用法、选择与循环以及函数设计与使用、lambda表达式用法、字符串与正则表达式应用、面向对象编程、文本文件与二进制文件操作、目录操作与系统运维、异常处理结构。

微信公众平台开发入门

本套课程的设计完全是为初学者量身打造,课程内容由浅入深,课程讲解通俗易懂,代码实现简洁清晰。通过本课程的学习,学员能够入门微信公众平台开发,能够胜任企业级的订阅号、服务号、企业号的应用开发工作。 通过本课程的学习,学员能够对微信公众平台有一个清晰的、系统性的认识。例如,公众号是什么,它有什么特点,它能做什么,怎么开发公众号。 其次,通过本课程的学习,学员能够掌握微信公众平台开发的方法、技术和应用实现。例如,开发者文档怎么看,开发环境怎么搭建,基本的消息交互如何实现,常用的方法技巧有哪些,真实应用怎么开发。

机器学习初学者必会的案例精讲

通过六个实际的编码项目,带领同学入门人工智能。这些项目涉及机器学习(回归,分类,聚类),深度学习(神经网络),底层数学算法,Weka数据挖掘,利用Git开源项目实战等。

eclipseme 1.7.9

eclipse 出了新的eclipseme插件,官方有下载,但特慢,我都下了大半天(可能自己网速差)。有急需要的朋友可以下哦。。。

Spring Boot -01- 快速入门篇(图文教程)

Spring Boot -01- 快速入门篇 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到【慕课网】手机 app,去找【Spring Boot 2.0 深度实践】的课程,令人开心的是,课程完全免费! 什么是 Spring Boot? Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot...

HoloLens2开发入门教程

本课程为HoloLens2开发入门教程,讲解部署开发环境,安装VS2019,Unity版本,Windows SDK,创建Unity项目,讲解如何使用MRTK,编辑器模拟手势交互,打包VS工程并编译部署应用到HoloLens上等。

最简单的倍频verilog程序(Quartus II)

一个工程文件 几段简单的代码 一个输入一个输出(50Mhz倍频到100Mhz)

计算机组成原理实验教程

西北工业大学计算机组成原理实验课唐都仪器实验帮助,同实验指导书。分为运算器,存储器,控制器,模型计算机,输入输出系统5个章节

4小时玩转微信小程序——基础入门与微信支付实战

这是一个门针对零基础学员学习微信小程序开发的视频教学课程。课程采用腾讯官方文档作为教程的唯一技术资料来源。杜绝网络上质量良莠不齐的资料给学员学习带来的障碍。 视频课程按照开发工具的下载、安装、使用、程序结构、视图层、逻辑层、微信小程序等几个部分组织课程,详细讲解整个小程序的开发过程

面试了一个 31 岁程序员,让我有所触动,30岁以上的程序员该何去何从?

最近面试了一个31岁8年经验的程序猿,让我有点感慨,大龄程序猿该何去何从。

基于RSA通信密钥分发的加密通信

基于RSA通信密钥分发的加密通信,采用pycrypto中的RSA、AES模块实现

不同变质程度煤尘爆炸残留气体特征研究

为分析不同变质程度煤尘爆炸残留气体成分的特征规律,利用水平管道煤尘爆炸实验装置进行了贫瘦煤、肥煤、气煤、长焰煤4种不同变质程度的煤尘爆炸实验,研究了不同变质程度煤尘爆炸后气体残留物含量的差异,并对气体

设计模式(JAVA语言实现)--20种设计模式附带源码

课程亮点: 课程培训详细的笔记以及实例代码,让学员开始掌握设计模式知识点 课程内容: 工厂模式、桥接模式、组合模式、装饰器模式、外观模式、享元模式、原型模型、代理模式、单例模式、适配器模式 策略模式、模板方法模式、观察者模式、迭代器模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式 课程特色: 笔记设计模式,用笔记串连所有知识点,让学员从一点一滴积累,学习过程无压力 笔记标题采用关键字标识法,帮助学员更加容易记住知识点 笔记以超链接形式让知识点关联起来,形式知识体系 采用先概念后实例再应用方式,知识点深入浅出 提供授课内容笔记作为课后复习以及工作备查工具 部分图表(电脑PC端查看):

MySQL数据库面试题(2020最新版)

文章目录数据库基础知识为什么要使用数据库什么是SQL?什么是MySQL?数据库三大范式是什么mysql有关权限的表都有哪几个MySQL的binlog有有几种录入格式?分别有什么区别?数据类型mysql有哪些数据类型引擎MySQL存储引擎MyISAM与InnoDB区别MyISAM索引与InnoDB索引的区别?InnoDB引擎的4大特性存储引擎选择索引什么是索引?索引有哪些优缺点?索引使用场景(重点)...

软件测试2小时入门

本课程内容系统、全面、简洁、通俗易懂,通过2个多小时的介绍,让大家对软件测试有个系统的理解和认识,具备基本的软件测试理论基础。 主要内容分为5个部分: 1 软件测试概述,了解测试是什么、测试的对象、原则、流程、方法、模型;&nbsp; 2.常用的黑盒测试用例设计方法及示例演示;&nbsp; 3 常用白盒测试用例设计方法及示例演示;&nbsp; 4.自动化测试优缺点、使用范围及示例‘;&nbsp; 5.测试经验谈。

几率大的Redis面试题(含答案)

本文的面试题如下: Redis 持久化机制 缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存降级等问题 热点数据和冷数据是什么 Memcache与Redis的区别都有哪些? 单线程的redis为什么这么快 redis的数据类型,以及每种数据类型的使用场景,Redis 内部结构 redis的过期策略以及内存淘汰机制【~】 Redis 为什么是单线程的,优点 如何解决redis的并发竞争key问题 Red...

手把手实现Java图书管理系统(附源码)

【超实用课程内容】 本课程演示的是一套基于Java的SSM框架实现的图书管理系统,主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的java人群。详细介绍了图书管理系统的实现,包括:环境搭建、系统业务、技术实现、项目运行、功能演示、系统扩展等,以通俗易懂的方式,手把手的带你从零开始运行本套图书管理系统,该项目附带全部源码可作为毕设使用。 【课程如何观看?】 PC端:https://edu.csdn.net/course/detail/27513 移动端:CSDN 学院APP(注意不是CSDN APP哦) 本课程为录播课,课程2年有效观看时长,大家可以抓紧时间学习后一起讨论哦~ 【学员专享增值服务】 源码开放 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化

jsp+servlet入门项目实例

jsp+servlet实现班级信息管理项目

winfrom中嵌套html,跟html的交互

winfrom中嵌套html,跟html的交互,源码就在里面一看就懂,很简单

Java面试题大全(2020版)

发现网上很多Java面试题都没有答案,所以花了很长时间搜集整理出来了这套Java面试题大全,希望对大家有帮助哈~ 本套Java面试题大全,全的不能再全,哈哈~ 一、Java 基础 1. JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境。 JRE:Java Runtime Environ...

python实现数字水印添加与提取及鲁棒性测试(GUI,基于DCT,含测试图片)

由python写的GUI,可以实现数字水印的添加与提取,提取是根据添加系数的相关性,实现了盲提取。含有两种攻击测试方法(高斯低通滤波、高斯白噪声)。基于python2.7,watermark.py为主

Xshell6完美破解版,亲测可用

Xshell6破解版,亲测可用,分享给大家。直接解压即可使用

你连存活到JDK8中著名的Bug都不知道,我怎么敢给你加薪

CopyOnWriteArrayList.java和ArrayList.java,这2个类的构造函数,注释中有一句话 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public ArrayList(Collection&lt;? ...

相关热词 c#怎么获得线程名 c# usb 采集器 c# sort() c#面对对象的三大特性 c# 打印 等比缩放 c#弹出右键菜单 c# 系统托盘图标 c# 键值对 键可以重复 c# 鼠标移上去提示 c#结构体定义
立即提问