Skip to content

Instantly share code, notes, and snippets.

@AndisGrossteins
Last active January 8, 2021 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndisGrossteins/538dc7b3aa5243bf67309d751bbabf38 to your computer and use it in GitHub Desktop.
Save AndisGrossteins/538dc7b3aa5243bf67309d751bbabf38 to your computer and use it in GitHub Desktop.
Start Linux daemons (services) in WSL 2 and WSL 1 using Windows Task Scheduler on Windows user logon
#!/bin/bash
# Start Linux daemons by starting WSL on user logon
#
# Usage:
# 1) Edit and save this bash shell script as /root/.bin/start-services.sh AND MAKE IT EXECUTABLE (chmod +x)
# (or wherever but remember to use the correct path in Scheduled task argumrents)
#
# 2) Set up a Windows Scheduled task to run this script via wsl.exe on user logon;
# Action - Start Program
# Program/script: C:\Windows\System32\wsl.exe
# Arguments: -u root -e /root/.bin/start-services.sh
#
# Note #1: The `-u root` part is needed to run the script as root user. Otherwise it won't work.
#
# Note #2: Be aware that if you're running a local DNS server in Windows
# (dnscrypt-proxy or Acrylic DNS Proxy) on TCP port 53, and *WSL 2*,
# you'll also have to stop those DNS service(s) before running wsl.exe or WSL2 will fail to start.
# Space-delimited list of daemons to start
DAEMONS="rsyslog dbus cron atd ssh nullmailer"
# Log file to later check what's gone down (or up)
LOG_FILE="/tmp/start_services_$(date +%F).log"
echo "Starting services $(date --rfc-3339=ns)" | tee -a $LOG_FILE
for daemon in $DAEMONS; do
printf "Trying to start '%s'" $daemon
service $daemon start 2>&1 | tee -a $LOG_FILE
RC=$?
echo "$(date --rfc-3339=ns) result: $RC" | tee -a $LOG_FILE
done
echo "Done at $(date --rfc-3339=ns)" | tee -a $LOG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment