Jump to content
xisto Community
Feelay

Make A Script Run Even If No User Is Online

Recommended Posts

Hey! Is there any way to make a script run, even if no user is online. Because at the moment, my scripts run, only when a user is online.And another thing:How can i make the following:(this is just an example)mysql_query"SELECT maxhp FROM users WHERE username = 'allusers'";How can I select all users maxhp, in the same query?Thanks //Feelay

Share this post


Link to post
Share on other sites

About the query, you have to do this:SELECT maxhp FROM usersThis returns a list with all the maxhp's from every user in the database. You can run through this list using while or foreach. Check out example #2 here:http://us2.php.net/manual-lookup.php?pattern=mysql_queryYour〈=en&scope=404quickref first problem can be solved using cron-jobs, but I've never used them so I don't know how you can configure them :) .

Share this post


Link to post
Share on other sites

Is cronjobs the only solution?and I have tried the query you gave me. But then, when I want to make every users hp to maxhp, everyone gets the highest maxhp in the table..

Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

Is cronjobs the only solution?

More or less, cron job is the only reliable solution. I've came across this before when I was developing a web based hotel management software, where I need to charge each room with daily charges pass midnight. The only way to guarantee that it's charge, or the code to charge runs properly and on time is by using cron. Of cause this is only for linux based machine or server.

 

If you're running a windows server or running on your local windows machine, you can use Schedule Task, and set the script to recurring. The only set back is that you need to start the task manually for the first time if you happen to restart your pc. I do not know of any other workaround for that, so I wrote a .Net System Service, which runs in the background and check for schedule to run, much like cron, and it will run even without login.

 

For cron, you can refer to -> https://en.wikipedia.org/wiki/Cron

 

Cron is the service that execute the scheduled task, while you need use crontab to add/edit/remove the tasks from cron.

 

If you're referring to Xisto' hosting, you can access "Cron Jobs" from cpanel. Choose standard, the rest is quite straight forward.

 

The command, you can either call php and pass the path to the script, or for my case I use wget, which fetch the output from an URL. So I wrote my script to output some status or response, wget will fetch it, and from the command, I redirect the output to a log file. That way I can easily keep track of the output. For cpanel, you can also choose to have the output send to your email address. But I would rather choose to check back from time to time instead of having my mail box filled up with logs(my script needs to be run every minutes).

wget -q -O- http://login.astahost.com/myscript.php >> /home/login/log/script.log
More or less like that, I can't remember the exact command. Anyway, I chose to use wget instead of php(commandline version), due to the fact that my script can't run properly in commandline, can't figure out what's wrong, so I chose the easier method.

Share this post


Link to post
Share on other sites

Forgotten to mention the less reliable ways.

1. Use https://www.webcron.org/index.php?〈=en. It wil lcall your URL/script at the preset time

2. Do the same as webcron, but from your own pc, or someone else pc which is one 24/7, using either schedule task or cron if you're running linux

3. Make the script intelligent, and call it from every link possible from your website, or from ajax scripted on our client side. You need to keep track of the execution of the script, so it will skip when it doesn't need to be run. Your site will need to have certain amount of traffic in order to use this method, and if not done properly, it will bog down the server. Also, it doesn't guarantee execution of the script at exactly the right timing.

I don't recommend using any of these methods, unless you really have no choice, especially the 3rd one.

Share this post


Link to post
Share on other sites

Just curious, but is there a particular reason you don't want to use a cron job? Using php for maintenance without user interaction might be tricky since usually it only runs when a user is accessing the site. however if their is a significant reason that you dont want to or can't use a cron job then I can see why it'd be worth looking into.

Share this post


Link to post
Share on other sites

Just curious, but is there a particular reason you don't want to use a cron job? Using php for maintenance without user interaction might be tricky since usually it only runs when a user is accessing the site. however if their is a significant reason that you dont want to or can't use a cron job then I can see why it'd be worth looking into.

There're cases where we run windows server, which doesn't have cron job. I would always use cron job on linux machine. Some hosting might not provide access to cron job.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.