No such file or directory error when trying to execute startup script in Debian

26,809

You probably have a carriage return (^M) at the end of your #! line.

The format of the #! line is very strict, and carriage return is not allowed there, unless your interpreter is actually called /bin/bash^M

There will never be carriage returns in a file created with a proper unix editor, unless you go out of your way to add them.

When editing an existing file that already uses CRLF line endings, the carriage returns might be hidden from you. For example, vim does that. But it also says [dos] in the status line to warn you that the file is in DOS format. You can then say :set fileformat=unix and save the file to convert it.

Share:
26,809

Related videos on Youtube

Sam Vanhoutte
Author by

Sam Vanhoutte

Updated on September 18, 2022

Comments

  • Sam Vanhoutte
    Sam Vanhoutte almost 2 years

    Very new to Debian (Raspbian), and I'm struggling on this one for a few days. I have a startup script that I want to execute at startup.

    I have executed the following commands, to make the script executable and to add it with the default parameters to the startup sequence.

    sudo chmod 755 /etc/init.d/testsam
    sudo update-rc.d testsam defaults
    

    When trying to test the script, I execute the following:

    sudo /etc/init.d/testsam start
    

    But when doing so, I get an error: unable to execute /etc/init.d/testsam: No such file or directory.

    I minimized the script to the very basic, but still don't have a clue of the actual reason. I hope someone can point me out to the right solution? This is the script at current.

    #! /bin/bash
    
    # /etc/init.d/testsam
    
    case "$1" in
     start)
            #echo "starting script"
            ;;
     stop)
            #echo "stopping script"
            ;;
     *)
            #echo "Usage: /etc/init.d/testsam {start|stop}"
            exit 1
             ;;
    esac
    
    exit 0
    

    Thanks for any help

  • Jenny D
    Jenny D about 10 years
    To verify whether this is in fact the problem, do cat -v /etc/inti.d/testsam. If you have an erroneous carriage return, it will show up as ^M.
  • Sam Vanhoutte
    Sam Vanhoutte about 10 years
    thank for the fast help! i had received the file through the wget command and it was indeed a windows based file.
  • user33777
    user33777 over 8 years
    On some servers I could use dos2unix but what do you do when the host doesn't allow that command to wipe all instances of ^M?
  • Al Belsky
    Al Belsky over 8 years
    Another way to fix the issue: sed -i -e 's/\r//g' /path/file