我们使用以下命令启动 Java 进程:
/usr/bin/java -classpath /home/somejavadaemon/Java/classes:/home/somejavadaemon/Java/classes/mail.jar:/home/somejavadaemon/Java/classes/commons-logging-1.2.jar:/home/somejavadaemon/Java/classes/httpclient-4.5.2.jar:/home/somejavadaemon/Java/classes/httpcore-4.4.4.jar core.Main configFile=/home/somejavadaemon/Java/config.cfg
当从用户的 shell ( somejavadaemon
) 启动时,一切都很好 - 守护进程正在工作,并且做它应该做的事情。
现在,我们要从 Systemd 单元文件中执行此操作:
[Unit]
Description=somejavadaemon service
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
[Service]
User=somejavadaemon
Group=somejavadaemon
Environment=JAVA_HOME=/usr/lib/jvm/java-8-oracle
Environment=J2SDKDIR=/usr/lib/jvm/java-8-oracle
Environment=DERBY_HOME=/usr/lib/jvm/java-8-oracle/db
Environment=J2REDIR=/usr/lib/jvm/java-8-oracle/jre
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
ExecStart=/usr/bin/java -classpath /home/somejavadaemon/Java/classes:/home/somejavadaemon/Java/classes/mail.jar:/home/somejavadaemon/Java/classes/commons-logging-1.2.jar:/home/somejavadaemon/Java/classes/httpclient-4.5.2.jar:/home/somejavadaemon/Java/classes/httpcore-4.4.4.jar core.Main configFile=/home/somejavadaemon/Java/config.cfg
[Install]
WantedBy=multi-user.target
根据 Systemd 的说法,服务启动 - 进程在内存中。但是,当我查看其状态时,我看到以下内容:
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:717) at whois.WhoisThreadPool$WhoisThread.start(WhoisThreadPool.java:319) at whois.WhoisThreadPool.init(WhoisThreadPool.java:42) at whois.WhoisThreadPool.<init>(WhoisThreadPool.java:36) at whois.Whois.<init>(Whois.java:49) at core.Main.main(Main.java:110)
为什么会发生这种情况?从 Systemd 启动与从用户 shell 启动有何不同?这与 cron 相同,我们尝试将其放在那里,取得了类似的成功。