Running a program for 'x' duration?

xuanbao · · 558 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m currently using PHP to execute a shell command to run a go script. (Form here: <a href="http://pastebin.com/9sXYCcx2" rel="nofollow">http://pastebin.com/9sXYCcx2</a> )</p> <p>I want this go script to work in the background of my site of &#39;x&#39; period(10 mins) of time after it&#39;s executed. </p> <p>How would I go about doing this? I also want the script to be able to handle multiple instances, such as using &#34;screen&#34; could do, where it is a separate instance and you can run the same program incoherently.</p> <p>And after 10 minutes of running, I want the program to exit. Here&#39;s the go script I&#39;m using for this question: <a href="http://pastebin.com/bHu4s1C5" rel="nofollow">http://pastebin.com/bHu4s1C5</a></p> <p>Any help is appreciated, thanks. :)</p> <hr/>**评论:**<br/><br/>Vlir: <pre><p>Look into the timeout command </p></pre>ninetalesz: <pre><p>thanks for the input! i&#39;m actually using a public source script, so could you hold my hand through where I would add this? I would presume at the beginning as it would be &#34;timeout after x&#34;, but I want to be sure. Appreciate it! (btw, new to the language, haha)</p></pre>Vlir: <pre><p>Was referring to the Unix timeout command.</p> <p>E.g</p> <p>timeout 10m ./program</p></pre>ninetalesz: <pre><p>thank you :)</p> <p>Do you have any idea why it wouldn&#39;t run when I filled out the form? I presume the commands are being passed to the terminal because it&#39;s not spitting an error at me &amp; the parameters fit the Go script. I tried to make it run in the background(as seen in the paste) but it won&#39;t work when I pass commands on my server via the web-page.</p> <p>I talked over it with a friend of mine and he said it could potentially be that apache doesn&#39;t have the capacity to run root commands to begin with. Do you think this would be the case? </p> <p>Thanks for your input!</p></pre>Vlir: <pre><p>The webserver would need permission to execute the file.</p></pre>ninetalesz: <pre><p>Appreciate the info.</p></pre>tiberiousr: <pre><p>You appear to be passing text form input to a system call with no taint checking. That&#39;s seriously bad practice and creates a massive attack vector for malicious hackers. Anyone could just drop a fork bomb in your &#39;name&#39; variable and bring down your server or worse. </p> <p>With regards to the Go script have you considered using the flag package to better parse cli arguments to your script? It&#39;d probably be cleaner.</p> <p>Also have you considerd not using PHP at all? It&#39;s an <a href="https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/" rel="nofollow">awful language</a> and there&#39;s no reason you couldn&#39;t do all of the above in one piece of Go code.</p></pre>ninetalesz: <pre><blockquote> <p>You appear to be passing text form input to a system call with no taint checking. That&#39;s seriously bad practice and creates a massive attack vector for malicious hackers. Anyone could just drop a fork bomb in your &#39;name&#39; variable and bring down your server or worse.</p> </blockquote> <p>My plan was to modify my PHP to check the name variable. This is just for my basic testing to begin with. But now that you&#39;ve mentioned it, I could consider doing it all in one go script.</p> <p>Thing is I&#39;m fairly newbish to both PHP and Go, I&#39;m trying to ease into developing more, but the <a href="https://cs50.harvard.edu/" rel="nofollow">CS50</a> course I&#39;m taking isn&#39;t all that rushing.(not to mention it&#39;s C, but I&#39;ve seen a lot of the basic structure follows it).</p> <p>Anyways, I do appreciate your input and I&#39;ll look more into the flag package and hopefully a better way to optimize it, guess I&#39;ll be hitting the documentation.</p></pre>nubunto: <pre><p>Maybe something like:</p> <pre><code>import &#34;time&#34; import &#34;os&#34; func main() { select { default: myCode() case &lt;-time.After(10 * time.Minute): return } } </code></pre> <p>?</p></pre>ninetalesz: <pre><p>Would this run everything past the case &lt;-time.After ?</p> <p>So like in my script I have the following block of code:</p> <pre><code>for _, nickname := range nicknames() { if conn, err := kahoot.NewConn(gamePin); err != nil { fmt.Fprintln(os.Stderr, &#34;failed to connect:&#34;, err) os.Exit(1) } else { defer conn.GracefulClose() conn.Login(nickname) } } </code></pre> <p>If I added the function you&#39;re suggesting to say, the top of my program, would it run the rest of the program diligently until that 10 minute timer was reached? Thank you for the input, nonetheless!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

558 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传