C# mon服务ubuntu守护进程服务启动失败
我在Ubuntu12.04 LTS上使用mono develop编写了一个带有C# mon服务ubuntu守护进程服务启动失败,c#,ubuntu,mono,C#,Ubuntu,Mono,我在Ubuntu12.04 LTS上使用mono develop编写了一个带有FileSystemWatcher的服务。该服务运行正常,但当我尝试使用脚本作为ubuntu守护进程运行它时,我的日志文件显示该服务正在运行,但FileSystemWatcher没有引发事件,尽管当我双击脚本运行时,该服务运行正常。 以下是我的OnStart方法: [PermissionSet(SecurityAction.Demand, Name="FullTrust")] protected
FileSystemWatcher
的服务。该服务运行正常,但当我尝试使用脚本作为ubuntu守护进程运行它时,我的日志文件显示该服务正在运行,但FileSystemWatcher
没有引发事件,尽管当我双击脚本运行时,该服务运行正常。以下是我的
OnStart
方法:
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
protected override void OnStart (string[] args)
{
try
{
WriteLog("dir Srvc On Start");
string d = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
watcher = new FileSystemWatcher ();
watcher.Path = d;
watcher.NotifyFilter =
NotifyFilters.DirectoryName | NotifyFilters.FileName |
NotifyFilters.LastAccess | NotifyFilters.LastWrite;
watcher.IncludeSubdirectories = false;
watcher.Filter = "*.txt";
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.Error += new ErrorEventHandler (OnError);
watcher.EnableRaisingEvents = true;
}
catch(Exception ex)
{
WriteLog("On Startup Exeption: "+ex.ToString());
}
}
这是我的OnCreated
method:
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
private void OnCreated(Object O, FileSystemEventArgs e)
{
try
{
WriteLog(e.FullPath.ToString() + " " + e.Name.ToString() + " "
+ e.ChangeType.ToString());
string outputdir = "OutPut";
string d = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string outdir = Path.Combine(d,outputdir);
Directory.CreateDirectory (outdir);
string outputfile = Path.Combine (outdir,e.Name);
File.Move(e.FullPath,outputfile);
}
catch(Exception ex)
{
WriteLog("On Created Exeption: "+ex.ToString());
}
}
以及我放在rc2.d目录下的可执行脚本:
#!/bin/sh
mono-service -l:/dir/forlock/service.lock /dir/forsrvc/dirWatchSrvc.exe
另外,我是ubuntu新手,对ubuntu中的脚本编写不太了解
已更新
我更新了代码,但它仍然不起作用这可能不是实际错误,但您使用的是Path。组合错误。正确的用法应该是Path.Combine(dir、towatch、outputdir)。该方法的要点是不使用硬编码的目录分隔符,同时不必担心要加入的字符串变量中的目录分隔符,因此,类似'C:\\\test\\testdfile.exe\'的情况不会发生。感谢Steffen的回复,但当我通过双击运行脚本时,此代码仍然有效。唯一的问题是,当此脚本通过操作系统引导序列运行时,服务正在运行,但文件系统监视程序事件没有出现,或者我通过sudo/etc/init.d/myscript运行它开始