我有一个简单的Perl脚本,它使用无限循环作为Linux守护进程运行.它每10秒连接一个数据库以执行一个进程.
while (1)
{
# THIS LINE WILL KILL THE SCRIPT IF IT FAILS
my $DB=DBI->connect("dbi:Sybase:server=myserver","user","password");
. . . do something . . .
sleep (10);
}
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
我们在c中有一个Linux守护进程和一个bash脚本来启动它.由于某些配置文件错误,守护程序有时无法启动,但脚本报告守护程序已成功启动.脚本的片段如下所示,有人可以告诉我脚本有什么问题吗?
...
case "$1" in
start)
echo -n "Starting Demo Daemon: "
sudo -u demouser env DEMO_HOME=$DEMO_HOME /usr/local/demouser/bin/democtl startup > /dev/null 2> /dev/null
if [ "$?" = "0" ]; then
echo_success
else
echo_failure
fi
echo
;;
...
Run Code Online (Sandbox Code Playgroud)
谢谢!
/bin/sh: /Users/alex/filename.py: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我已经设置了一个cronjob,它在正确的时间运行,但脚本没有被执行.当我检查邮件时,我收到了该错误.脚本和cronjob文件都在:
/Users/alex
Run Code Online (Sandbox Code Playgroud)
我的cronjob文件看起来像这样:
45 19 * * * /Users/alex/filename.py
Run Code Online (Sandbox Code Playgroud)
我知道该脚本有效,因为我可以从终端手动启动它.
我搞砸了哪里?
我写了一个管理脚本,尾随一个heroku日志,每n秒,它总结平均值,并通知我,如果我跨越一定的门槛(是的,我知道并喜欢新的遗物 - 但我想做自定义的东西).
这是整个脚本.
我从未成为IO和线程的主人,我想知道我是否犯了一个愚蠢的错误.我有几个守护程序线程while(true){}可能是罪魁祸首.例如:
# read new lines
f = File.open(file, "r")
f.seek(0, IO::SEEK_END)
while true do
select([f])
line = f.gets
parse_heroku_line(line)
end
Run Code Online (Sandbox Code Playgroud)
我使用一个守护进程来监视日志的新行,另一个用于定期汇总.
有人看到一种方法可以降低处理器密集度吗?
怎么样?可能吗?如果不是我的替代品?
可能是python守护进程?
编辑:我的目标是在互联网(在线)中使用自定义协议.
我有一个要求,我想创建一个5线程的池,现在我想从这5个线程中的1个线程作为一个daemon线程,当这个特定的1个线程变为守护线程时,那么我想分配一些任务到那个与任何服务相关的守护程序线程,当java程序退出时我可以在窗口任务管理器中检查特定的守护程序线程是否还在执行该任务.请告知如何实现该目的..!因为我被困在这......
下面是我的代码......
public class StoppingThread extends Thread //extend thread class
{
// public synchronized void run()
//synchronized (this)
private volatile boolean Completed = false;
public void setCompleted() {
Completed = true;
}
public void run()
{
for(int i=0;i<20 && !Completed;++i) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(500);
System.out.print(i +"\n"+ "..");
} catch(Exception e) {
e.printStackTrace();
}
}
}
public static void main(String... a)
{
StoppingThread x = new StoppingThread();
StoppingThread y = new StoppingThread();
x.start();
x.setName("first");
x.setCompleted(); // Will complete …Run Code Online (Sandbox Code Playgroud) 如果我从我的程序(非守护程序进程)创建一个守护程序线程,堆和perm gen内存空间是否与新线程共享或是否重新分配?
如果守护程序线程有自己的空间,那么在新线程的创建中是否遵守了最大堆大小等JVM内存调整算法?
我似乎无法找到一种方法让一个C#服务作为debian中的"服务"运行.我究竟做错了什么?
我跟着这个帖子从MSDN创建一个样本窗口服务:http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx
我可以在我的Windows机器上运行该服务,启动和停止服务,并看到它写入MyNewLog.
然后我将它(.exe文件)复制到我的debian机器并尝试使用(作为root)mono-service MyNewService.exe运行它
Syslog告诉我,服务已经开始了!
我没有错误,我在系统中看不到任何新创建的日志文件.我究竟做错了什么?
如果有帮助,这里是代码:Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace MyNewService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{
new MyNewService()
};
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Service.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace MyNewService
{
public partial class MyNewService …Run Code Online (Sandbox Code Playgroud) 我想把主线程作为守护程序线程,但它告诉我IllegalThreadStateException.有没有办法做到这一点?
public class DeamonThreads {
public static void main(String[] args) {
System.out.println("Main Started");
System.out.println("Thread type deamon = " + Thread.currentThread().isDaemon());
Thread.currentThread().setDaemon(true);
System.out.println("Thread type deamon = " + Thread.currentThread().isDaemon());
System.out.println("Main End");
}
}
Output
Main Started
Thread type deamon = false
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Thread.java:1367)
at com.threads.DeamonThreads.main(DeamonThreads.java:8)
Run Code Online (Sandbox Code Playgroud) 有类似的问题,但我所追求的是文件存在的实时检测.
我有2个linux盒子/ PC.
[Linux Box 1] - >生成一个名为host的文件(手动执行)
[Linux Box 2] - >有一个守护进程,每2秒休眠一次,检查预期的文件是否存在.如果它存在,它将执行我的ssh命令(当然会显示一个文本显示它被执行了bla bla bla).
PERL SCRIPT:
ctr:
if (-e $file) { system ("ssh $script $host.$display"); }
sleep(2);
goto ctr;
Run Code Online (Sandbox Code Playgroud)
问题 问题是,我似乎无法确定文件是否实时存在,即使它已经存在.
方框1
[machine01:/home/machine01]% /MACHINE/TOOLS/perl/app/host_gen
> executed at 08/21/2015 21:40:32
Run Code Online (Sandbox Code Playgroud)
方框2
Sleeping for 2 seconds [08/21/2015 21:40:22]
Sleeping for 2 seconds [08/21/2015 21:40:24]
Sleeping for 2 seconds [08/21/2015 21:40:26]
Sleeping for 2 seconds [08/21/2015 21:40:28]
Sleeping for 2 seconds [08/21/2015 21:40:30]
Sleeping for 2 seconds [08/21/2015 21:40:32] **<-expected …Run Code Online (Sandbox Code Playgroud)