<p>I'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 'x' period(10 mins) of time after it'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 "screen" 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's the go script I'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'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 "timeout after x", 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't run when I filled out the form? I presume the commands are being passed to the terminal because it's not spitting an error at me & the parameters fit the Go script. I tried to make it run in the background(as seen in the paste) but it won'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'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's seriously bad practice and creates a massive attack vector for malicious hackers. Anyone could just drop a fork bomb in your 'name' 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'd probably be cleaner.</p>
<p>Also have you considerd not using PHP at all? It's an <a href="https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/" rel="nofollow">awful language</a> and there's no reason you couldn'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's seriously bad practice and creates a massive attack vector for malicious hackers. Anyone could just drop a fork bomb in your 'name' 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've mentioned it, I could consider doing it all in one go script.</p>
<p>Thing is I'm fairly newbish to both PHP and Go, I'm trying to ease into developing more, but the <a href="https://cs50.harvard.edu/" rel="nofollow">CS50</a> course I'm taking isn't all that rushing.(not to mention it's C, but I've seen a lot of the basic structure follows it).</p>
<p>Anyways, I do appreciate your input and I'll look more into the flag package and hopefully a better way to optimize it, guess I'll be hitting the documentation.</p></pre>nubunto: <pre><p>Maybe something like:</p>
<pre><code>import "time"
import "os"
func main() {
select {
default:
myCode()
case <-time.After(10 * time.Minute):
return
}
}
</code></pre>
<p>?</p></pre>ninetalesz: <pre><p>Would this run everything past the case <-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, "failed to connect:", err)
os.Exit(1)
} else {
defer conn.GracefulClose()
conn.Login(nickname)
}
}
</code></pre>
<p>If I added the function you'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>
![](https://static.golangjob.cn/static/img/banner.png)