Running scheduled jobs e.g hourly on Linux VM / WSL / Host Linux

  1. jimbo45's Avatar
    Posts : 10,941
    Windows / Linux : Arch Linux

    Running scheduled jobs e.g hourly on Linux VM / WSL / Host Linux

    Hi folks

    for those who have NAS (Linux) type servers / Linux VM's or Host Linux systems it's dead easy to set up jobs that run say every hour or whatever - use for this would be scheduled backups etc etc.

    I've got a simple 1 line example here of running an email to myself every hour --simply sends my external IP address -- so I can logon remotely to server even if ISP changes the IP address (which they often do --rare to be given ISP static internet addresses). That way I don't need dyndns, teamviewer or other things like that but there's all sorts of commands you can schedule to run --backups would be one that immediately seems obvious to me - but always best to start simple !!!

    Create your executable bash / script file say in home directory and then link it to /usr/local/bin (via the ln command)
    and now create an entry in crontab (you can be a normal user - doesn't need admin or root privileges to edit your own crontab)

    first the actual email command (but can be any script / job you like) (for dig command install packages dnsutils and netcat)
    note BASH expects the #!/bin/bash in the ist line even though # is a comment line

    # Send email from bash
    # hrafn simple script to send simple email with your IP address (external)
    server="<email server>"
    port="<port>" ===< assume 25 -- this is for standard not auth not tls etc
    from="<your email address>"
    to="<email of receiver>"
    # get your public IP address
    myip="$(dig +short"
    # create message
    function mail_input {
    echo "ehlo $(hostname -f)"
    echo "MAIL FROM: <$from>"
    echo "RCPT TO: <$to>"
    echo "DATA"
    echo "From: <$from>"
    echo "To: <$to>"
    echo "Subject: Internet Addressl"
    echo " "
    echo "Your public internet address is "
    echo $myip
    echo " "
    echo "finished"
    echo "."
    echo "quit"

    mail_input | netcat $server $port || err_exit

    save as a .sh type of file and make executable and ln to /usr/local/bin

    Don't forget the echo "." line --not a typo this indicates to the mail server that it's end of input data stream.

    now the crontab table 1 line is all that's needed --google for crontab entries -- this is the simplest -run this job every hour on the hour 7 days a week. You can make much more specific - by time, by day by date etc etc.

    0 * * * * /usr/local/bin/automail

    SEEEMPLES !!"!!

    works in WSL as well

    If you need more complex email authorisations, attachments and password authentication etc it's easy to do -- beyond the scope of this post which was simply to show how easy it is to schedule any jobs you might want on NAS type server or if using the WSL on Windows.

    Update : Here's the mail that arrived in Outlook inbox at 14.00 (scheduled via the crontab) - approx 20 mins after I made this post.

    Running scheduled jobs  e.g hourly on Linux VM / WSL  / Host Linux-email.png

    Happy new year and "Cronning"

    Last edited by jimbo45; 01 Jan 2020 at 08:14. Reason: Added image of successful email via Crontab
      My Computer


  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 20:24.
Find Us

Windows 10 Forums