Reputation: 67
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!
Upvotes: 2
Views: 1884
Reputation: 648
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
Upvotes: 0
Reputation: 4353
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.
Upvotes: 2
Reputation: 6958
Any language would work. I can recommend the sledgehammer approach which would be Webmin.
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
Upvotes: 1