Executing background jobs - Permissible or not ?

academics17

New Member
Messages
3
Reaction score
0
Points
1
I need to run a PhP job in the background. This will collect data and populate a mysql database. When I am doing the same on my desktop machine, I have the luxury of "kill"ing it if it misbehaves but I am not sure whether on x10 -- without shell access -- I could do the same. Again, I do not want to create a runaway program that causes difficulty to others.

What would be a good way to strategy for this requirement within the constraints of a free x10 account.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You can have non-interactive processes that run as cron jobs. However, the high resource usage (HRU) restriction will still apply. If you're worried about a runaway PHP script, you can use set_time_limit. More generally, you can force a command to exit with:

Code:
#!/bin/bash

timeout=30
if [ $# -gt 0 ]; then
    timeout=$1
fi

/path/to/binary arg0 arg1 ... argN >$HOME/log/job.log 2>&1 &
sleep $timeout && kill $! 2>/dev/null

Replace the "/path/to/binary ... argN" with the command to run, save it as a file in ~/bin and set the cron job command to the file.

Even more generally, you can use the following script (saved as e.g. "~/bin/timelimit") to run an arbitrary command for a limited length of time. Make sure you escape any shell metacharacters when calling the script so they don't get interpreted before timelimit gets a chance to execute the command.
Code:
#!/bin/bash

timeout=30
if [ $1 -ne 0 2>/dev/null ] ; then
    timeout=$1
    shift
fi
eval "$*" &
sleep $timeout && kill $! 2>/dev/null
Example usage:

Code:
$HOME/bin/timelimit 60 cut -f 1 -d ' ' $HOME/data/activity \| sort -n \| uniq -c \>$HOME/logs/visit_counts
#or:
$HOME/bin/timelimit 60 "cut -f 1 -d ' ' $HOME/data/activity | sort -n | uniq -c >$HOME/logs/visit_counts"

For an example of how you can run multiple, specific commands with a timeout for all, read Alarms, Timeouts and Parallel Processes in Bash
 
Last edited:

qchimex1

New Member
Messages
1
Reaction score
0
Points
0
academics17 : did you find out how to do this? I have same requirement of running a PHP process in background continuously ( till its killed).
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
@qchimex1: you need to pay closer attention when reading, both to my post and the terms of service which you agreed to when you signed up. Both already address your post.
 
Last edited:
Top