0

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.

Eduard
  • 3,395
  • 8
  • 37
  • 62
  • You've probably got a `^M` carriage return char at the end of your shebang, since it says that `/bin/sh^M` doesn't exist. e.g. you built your script file on a Windows machine, and are executing it on a Unix-ish box. – Marc B Jul 04 '14 at 17:05

1 Answers1

0

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

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61