Page 1 of 1

Script for changing raspberry pi hostname during boot sequence

Posted: Thu May 03, 2018 4:08 pm
by Kimomaru
I have a script being used on an older raspberry pi that seems to work differently on a more current raspbian. This is for an inherited project, so please forgive the ignorance.

On the old raspberry pi, two scripts exist in the /etc/initd.d directory; rename.sh and hostname.sh. They look like this;

rename.sh;

Code: Select all

### BEGIN INIT INFO
# Provides:          Rename.sh
# Required-Start:    $local_fs $network $all
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Rename.sh
# Description:       Causes system to rename itself
### END INIT INFO


. /lib/lsb/init-functions

case "$1" in 
        start)
                log_action_msg "Enter new System Name:"
                read -p "New System Name: " name
                sed -i -e "s/pi01/$name/g" /etc/hostname
                sed -i -e "s/pi01/$name/g" /etc/hosts
                /etc/init.d/hostname.sh
                update-rc.d -f rename.sh remove
        ;;
        stop)
        ;;
        *)
        ;;
esac

exit 0
hostname.sh

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          hostname
# Required-Start:
# Required-Stop:
# Should-Start:      glibc
# Default-Start:     S
# Default-Stop:
# Short-Description: Set hostname based on /etc/hostname
# Description:       Read the machines hostname from /etc/hostname, and
#                    update the kernel value with this value.  If
#                    /etc/hostname is empty, the current kernel value
#                    for hostname is used.  If the kernel value is
#                    empty, the value 'localhost' is used.
### END INIT INFO

PATH=/sbin:/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start () {
    [ -f /etc/hostname ] && HOSTNAME="$(cat /etc/hostname)"

    # Keep current name if /etc/hostname is missing.
    [ -z "$HOSTNAME" ] && HOSTNAME="$(hostname)"

    # And set it to 'localhost' if no setting was found
    [ -z "$HOSTNAME" ] && HOSTNAME=localhost

    [ "$VERBOSE" != no ] && log_action_begin_msg "Setting hostname to '$HOSTNAME'"
    hostname "$HOSTNAME"
    ES=$?
    [ "$VERBOSE" != no ] && log_action_end_msg $ES
    exit $ES
}

do_status () {
    HOSTNAME=$(hostname)
    if [ "$HOSTNAME" ] ; then
        return 0
    else
        return 4
    fi
}

case "$1" in
  start|"")
    do_start
    ;;
  restart|reload|force-reload)
    echo "Error: argument '$1' not supported" >&2
    exit 3
    ;;
  stop)
    # No-op
    ;;
  status)
    do_status
    exit $?
    ;;
  *)
    echo "Usage: hostname.sh [start|stop]" >&2
    exit 3
    ;;
esac

:
By invoking update-rc.d rename.sh defaults and rebooting, this script will start on the next reboot before the login prompt appears (the desired behavior - this is for facilitating the staging of new raspberry pi units). No problems.

In rebuilding the image on a more current raspbian, this works different. It never shows up during boot and /var/log/syslog yields;

Code: Select all

May  3 08:13:02 debian systemd[459]: rename.service: Failed at step EXEC spawning /etc/init.d/rename.sh: Exec format error
May  3 08:19:08 debian systemd[466]: rename.service: Failed at step EXEC spawning /etc/init.d/rename.sh: Exec format error
May  3 08:27:01 debian systemd[450]: rename.service: Failed at step EXEC spawning /etc/init.d/rename.sh: Exec format error
This error is indicative of a formatting issue in rename.sh (which did not seem to be an issue in the previous build - the lack of #! /bin/sh at the very top.) When I add it, no change in behavior but now the logs indicate;

Code: Select all

May  3 08:30:39 debian rename.sh[467]: Enter new System Name:.
It does seem to be invoked, but it's not visible, there's no prompt anywhere, it just gets written to log. Can someone point me in the right direction?

Re: Script for changing raspberry pi hostname during boot sequence

Posted: Thu May 03, 2018 4:16 pm
by DougieLawson
Add

Code: Select all

#!/bin/bash
to the top of your script, that will eliminate the "exec format error".

Re: Script for changing raspberry pi hostname during boot sequence

Posted: Thu May 03, 2018 4:31 pm
by Kimomaru
As I mentioned, I had already done that.

Re: Script for changing raspberry pi hostname during boot sequence

Posted: Thu May 03, 2018 4:54 pm
by DougieLawson
Does /etc/init.d/rename.sh have the eXecutable bit?

sudo chmod 755 /etc/init.d/rename.sh

Re: Script for changing raspberry pi hostname during boot sequence

Posted: Fri May 04, 2018 8:55 am
by BBUK
The only time I have have seen this error is by pasting text into a SSH terminal window from Windows notepad. It was caused by DOS-style line endings.

Try converting the line endings to Unix style. Loads of ways of doing this (dos2unix for example). This perl one-liner also works:

Code: Select all

perl -pi -e 's/\r\n/\n/g' input.file
BBUK

(Edited, put wrong version of command in!)