Skip to content

Instantly share code, notes, and snippets.

@danmaq
Last active August 13, 2016 07:06
Show Gist options
  • Save danmaq/8825128e199c787b46ca61e4786447a8 to your computer and use it in GitHub Desktop.
Save danmaq/8825128e199c787b46ca61e4786447a8 to your computer and use it in GitHub Desktop.
/etc/rcX.d/AllScripts launcher for "Ubuntu on Windows". Usage: sudo rund4wsl.sh -l <runlevel> -o <start|stop>
#!/bin/bash
failed()
{
echo $1 1>&2
exit 1
}
usage()
{
failed "Usage: sudo $0 -l <runlevel> -o <start|stop>"
}
[ $# = 0 ] && usage
OPTION=`getopt -n $0 -o l:o:h -- "$@"`
[ $? != 0 ] && usage
eval set -- "$OPTION"
while true
do
case "$1" in
-l) ENABLED_RUN_LEVEL=true
RUN_LEVEL=$2
shift 2
;;
-o) ENABLED_DAEMON_OPTION=true
DAEMON_OPTION=$2
shift 2
;;
-h)
usage
;;
--)
shift
break
;;
esac
done
[ ! $ENABLED_RUN_LEVEL ] && usage
[ ! $ENABLED_DAEMON_OPTION ] && usage
TARGET="/etc/rc${RUN_LEVEL}.d"
[ ! -e $TARGET ] && failed "Invalid run level: $RUN_LEVEL"
for FILE in `find $TARGET -maxdepth 1 -perm -u=x \! -type d`
do
echo "$FILE $DAEMON_OPTION"
echo `$FILE $DAEMON_OPTION`
done
@danmaq
Copy link
Author

danmaq commented Aug 13, 2016

start_rund4wsl.cmd - Windows 側からログオン時に呼び出してください。

@if(0)==(0) ECHO OFF
@start /b wscript //nologo //E:JScript "%~f0" %*
GOTO :EOF
@end
new ActiveXObject("WScript.Shell").Run("bash.exe -c \"sudo rund4wsl.sh -l 3 -o start; sleep infinity\"", 0);

term_rund4wsl.cmd - Windows 側からログオフ時に呼び出してください。

@if(0)==(0) ECHO OFF
@start /b wscript //nologo //E:JScript "%~f0" %*
GOTO :EOF
@end
new ActiveXObject("WScript.Shell").Run("bash.exe -c \"sudo rund4wsl.sh -l 0 -o stop\"", 0);

予め visudo でパスワード省くようにしないとうまく動かないかも。それと chmod。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment