简体   繁体   中英

How to run php file in shell script infinite times

I'm new to linux system and I'm trying to make a PHP script to be ran infinite times. Note that I'm using Debian 7 .

So, I'm using a screen to open a window, so far so good, I have the worker.php file already running succsefully, and I need to make shell script which runs the php script infinite times.

So I've come up with this:

#!/bin/sh
for (( ; ; ))
do
    /usr/bin/php worker.php
    sleep 1
done

The problem is , when trying to run ./worker.sh in the screen , I get this error:

bash: ./worker.sh: /bin/sh^M: bad interpreter: No such file or directory

So I've stripped of the for , and replaced it with a simple echo , which results into the same error, so I've wrote this question because I don't know what's wrong, both sh or bash exist on the server, I'm wondering if the shebang is wrong but.. I have the automysqlbackup script which starts with the same shebang .

Do you have any idea what is wrong ? I'm just a newb.. don't really know much. If you're wondering why am I running a file every second, it's because this file serves as a commands processor from a queue in a game. And running it with cron every minute is too slow. MySQL triggers are not fitting my needs, so I'm forced doing this.

Regards.

From the message it looks like you have a <cr><lf> at the end of your shebang line (the #! one). As isn't a valid line end on debian unix (it is on windows and some other varieties of unix), it is being taken as part of the filename, and so the o/s can't find the program to run.

Fixing it would require something like this:

tr -d '\015' < worker.sh > worker_nocr.fixed

Also, as you're using bash as your shell, you might wish to change the shebang to use bash as well, or other things might not work which work fine when you type them in at the command prompt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM