Shell脚本使用密码创建目录并使其在系统启动时可运行

时间:2012-05-24 15:31:49

标签: bash shell ubuntu startup

我要做的是在启动时运行应用程序。所以我把它作为一项服务。即作为/etc/init.d下的shell脚本。我正在使用Ubuntu ..问题是我必须在/ var / run目录下创建一个文件夹,如果它不存在的话。以下是我的代码。

#!/bin/sh
set -e

start() {


if [ -d "/var/run/bucardo" ]; then
    echo "Directory Exists" 
    echo -n "Starting bucardo: "
    sudo bucardo_ctl start 
    echo "done."
else
    echo "Creating Directory" 
    sudo mkdir /var/run/bucardo
    echo -n "Starting bucardo: "
    sudo bucardo_ctl start 
    echo "done."
fi
}

stop() {
echo -n "Shutting down bucardo: "
sudo bucardo_ctl stop
echo "done."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 10
start
;;
*)

esac
exit 0

所以在/ var / run下创建一个文件夹,它是应用程序的默认文件夹。

如何在shell脚本中创建一个带密码的文件夹。这样我就可以检查文件夹并创建它。

以下是我在启动时运行脚本时遇到的错误。我使用以下命令来执行“chkconfig --level 234 bocardo”

以下是错误..

insserv: warning: script 'K01centrifyda' missing LSB tags and overrides
insserv: warning: script 'K01cups' missing LSB tags and overrides
insserv: warning: script 'K01acpi-support' missing LSB tags and overrides
insserv: warning: script 'bucardo' missing LSB tags and overrides
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'hwclock-save' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `hwclock-save'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `hwclock-save'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'ufw' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `ufw'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `ufw'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'lightdm' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `lightdm'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `lightdm'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'acpid' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `acpid'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `acpid'
insserv: warning: script 'centrifyda' missing LSB tags and overrides
insserv: warning: script 'adnisd' missing LSB tags and overrides
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'binfmt-support' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `binfmt-support'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `binfmt-support'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'plymouth-splash' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `plymouth-splash'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `plymouth-splash'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'network-interface' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `network-interface'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `network-interface'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'plymouth-log' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `plymouth-log'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `plymouth-log'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'alsa-restore' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `alsa-restore'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `alsa-restore'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'procps' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `procps'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `procps'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'udevtrigger' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `udevtrigger'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `udevtrigger'
insserv: warning: script 'cups' missing LSB tags and overrides
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'setvtrgb' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `setvtrgb'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `setvtrgb'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.
insserv: warning: script 'udev' missing LSB tags and overrides
insserv: Default-Start undefined, assuming empty start runlevel(s) for script `udev'
insserv: Default-Stop  undefined, assuming empty stop  runlevel(s) for script `udev'
The script you are attempting to invoke has been converted to an Upstart
job, but lsb-header is not supported for Upstart jobs.

很长...请帮帮我.. 提前谢谢..

根据彼得的建议...... 以下是在Ubentu中将其作为服务的链接.. http://jonathonhill.net/2009-04-23/auto-start-a-shell-script-on-ubuntu-server/

1 个答案:

答案 0 :(得分:3)

你没有说实际上是什么问题。是sudo mkdir要求输入密码吗? Init脚本通常需要以root身份运行(并且在正常启动期间),因此您根本不需要脚本中的sudo。在测试时,运行脚本时只需sudo

另外,考虑折叠目录存在条件,并使用mkdir -p,如果目录已经存在则不会出错。