2

I got some long running bash scripts on my Ubuntu 10.04 Server that are doing things like backups and database dumps.

I'm currently executing them via cron and mailing me the results. However it would be nice to trigger them manually throughout a Webinterface additionally. (I do not want to give any user SSH access so he can trigger the scripts)

In PHP, for instance, there is the exec Command which obtains the Script output AFTER the Script has finished. This is not useful since the Scripts last longer than the 30 Seconds Browser Timeout. (And I want to monitor the progress) So I'm currently stuck in finding a way on how to gather the Script output periodically or "tailing" and display it through JavaScript or Pagereloads or something like that.

As I noticed, Webmin for instance is somehow doing this when you run system commands like apt through their webinterface.

One way I could imagine is forking the script execution and redirecting the output to a file, whilst the Main Process is reading it on Page reload/ Ajax request. But this sounds somehow like a workaround :)

So, does anyone have a good solution for this problem? Doesn't need to be PHP, could also be python or perl or anything else on server side. Or is there a tool or library for such things?

Any ideas greatly appreciated!

achschneid
  • 67
  • 7
  • 1
    You might take a look at these PHP solutions: http://stackoverflow.com/questions/1281140/run-process-with-realtime-output-in-php – Steve Goodman Mar 19 '13 at 17:56

3 Answers3

2

I am not sure if you mean to require PHP in your solution, but if not…

In the past, I have used Jenkins for this kind of thing. You can use it as both a cron replacement and as a way to manually trigger scripts, all while keeping logs of what happens.

Jenkins can be set up such that some users only have permission to trigger jobs, as opposed to the Jenkins administrator who can do stuff like set up and edit jobs, etc.

And if you like getting email, Jenkins can do that too. If anything, the biggest downside to Jenkins is that it is super-flexible / configurable, and so not always obvious how best to set it up for one's particular needs.

danfuzz
  • 4,253
  • 24
  • 34
  • Ahh, good idea, I'm familiar with Jenkins, but didn't come up with using it for executing scripts. Anyways Jenkins might be a bit overhead for just triggering a script (and explain it to the users). But its a really good idea! – achschneid Mar 19 '13 at 17:55
  • hmmm always thought of jenkins as more of a CI code testing/ build server tool. – j_mcnally Mar 19 '13 at 18:02
  • me too, so I didn't catch this up either. But it has the cron functionality built in, and besides ant or maven targets its also possible to execute system commands. So this might be a somehow elegant way in doing this all together. I'll have a look how far I can restrict the anonymous user profile and then might give it a try with Jenkins! – achschneid Mar 19 '13 at 18:11
1

Any language would work. I can recommend the sledgehammer approach which would be Webmin.

http://www.webmin.com/

the advantage is what you asked for is baked in and you wouldnt have to program anything.

in php you should look at

buffers and flushing. You can basically output to the page as you recieve output.

http://php.net/manual/en/function.ob-start.php

see this related question for the popen sytax.

php shell_exec with realtime updating

Community
  • 1
  • 1
j_mcnally
  • 6,928
  • 2
  • 31
  • 46
  • Well, good point to use webmin if you actually want to do what webmin does :D but I don't want to give the users access to my system (which would be like giving them ssh access), so webmin propably is not the right approach. Thanks for your hints regarding PHP, I will take a look at this! – achschneid Mar 19 '13 at 17:58
  • 1
    i think you could possibly create a very limited user account and restrict what webmin could do.... it was just a thought ;) – j_mcnally Mar 19 '13 at 17:59
0

I did something similar ( quick and dirty but might not be very efficient ) I used a database log table to store the output ( from the back end script ) The webpage would then make periodic ajax requests to get only those logs that it has not fetched previously and display(append) it to the front end.

The database table was very simple with columns

id(auto increment )

log_text

script_identifier ( unique if you are tracking multiple scripts )

last_modified_time

output_displayed ( value = 0 tells that this output has not been read by the ajax )

A simple process would be like this Open a webpage and click a button to send an ajax request to start the back end script At every 5 seconds send another ajax request to see if there are unread log table rows If there are unread rows, mark them as read, fetch them and display

You can improve in many things like use apc or memcached instead of database table keep the log table small or dynamically create log tables for each script

itz_nsn
  • 648
  • 1
  • 8
  • 13