简体   繁体   English

C#控制台。 如何每10分钟重新启动一次应用程序?

[英]C# console. how to restart an application every 10 minutes?

I have a C# console application that needs to be restarted every 10 minutes. 我有一个C#控制台应用程序,需要每10分钟重新启动一次。 I was using a batch script to do that till now, but now I need to run this application on linux using mono. 到目前为止,我一直使用批处理脚本来执行此操作,但是现在我需要使用mono在Linux上运行此应用程序。 Any hints on how to do that? 有关如何执行此操作的任何提示? I don't want to create a bash script for this. 我不想为此创建一个bash脚本。 Maybe something in my c# code. 也许在我的C#代码中。 I've tried threading, timers and what not. 我试过线程,计时器和其他方法。

You can use a cron job to schedule it to run every 10 mins, eg: 您可以使用cron作业将其安排为每10分钟运行一次,例如:

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/ http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

You don't want to use a Bash script but I think it's a good and quickly solution. 您不想使用Bash脚本,但我认为这是一个很好且快速的解决方案。

In your crontab add something like : 在您的crontab中添加以下内容:

*/10 * * * * /script/pathname.sh 

On in the general crontab : 在一般的crontab中:

*/10 * * * * username /script/pathname.sh 

In the script, write something like that : 在脚本中,编写如下内容:

#!/bin/bash
killall myapp
/usr/bin/myapp

Don't forget the sha bang and don't forget to make it executable : 不要忘记shabang,也不要忘记使其成为可执行文件:

chmod u+x /usr/bin/myapp

You start new copy of your app and close current. 您启动应用程序的新副本并关闭当前版本。

string myApp = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
    System.Diagnostics.Process.Start(myApp);
    Environment.Exit(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM