Verification of how to periodically execute a script on a Linux server on Windows

We verified whether the operation can be performed according to the title. image.png

-Verification environment- Windows 10 2 virtualbox VMs (Centos7)

Technology used/function purpose of use
Task Manager For periodic execution of Linux server scripts from Windows
teraTermMacro To ssh connect to the Jump server and kick the Shell script
expect To kick a Shell script on the Target server from the Jump server
Windows bat Since it is inconvenient to set ttl directly in TaskManager, details will be explained later.

The setting will proceed in the reverse flow of the image. ① Target server ・ Install the shell script you want to process ・ Process to acquire the memory usage rate ② Jump server ・ Install expect and install shell script using its function) ③ Windows local PC ・ TeraTermMacro (created for ShellScript Kick of Jump server) ・ Windows bat ・ TaskManager periodic execution setting

1. Install Shellscript on Target server

Work target: Target server Purpose: Set up a shell script for the process you want to run (as a process to record memory usage)

$ cat memory-use-rate-check.sh
#!/bin/bash
# create file memory-use.log
if [[ ! -e /var/tmp/memory-use.log ]]; then
  touch /var/tmp/memory-use.log
  chmod 755 /var/tmp/memory-use.log
fi
# memory use rate check
MemTotal=`free | grep Mem: | awk -F " " '{print$2}'`
MemoFree=`free | grep Mem: | awk -F " " '{print$4}'`

MemUse=`expr $MemTotal - $MemoFree`
MemRate=`expr $MemUse "*" 100 / $MemTotal`

echo `date +%Y-%m-%d/%H:%M` >> /var/tmp/memory-use.log
echo  "Memory Used Rate:" $MemRate"%" >> /var/tmp/memory-use.log

Execution result: It's like recording the memory usage in a log file.

[vagrant@Target tmp]$ tail -n2 memory-use.log
2020-12-10/11:31 
Memory Used Rate: 16% 

2. Expect installation, shell script installation using that function.

Work target: Jump server Purpose: Remotely execute memory-use-rate-check.sh installed in 1 from the Jump server. 2-1. Install expect

[vagrant@Jump tmp]$ sudo yum install expect -y

2-2. Installed a shell script that uses the expect function (automates the dialogue response). Purpose: Kick memory-use-rate-check.sh of Jump → target.

[vagrant@Jump tmp]$ cat expect-for-target.sh
#!/bin/sh
expect -c "
set timeout 2
spawn env LANG=C /usr/bin/ssh [email protected]
expect \"password:\"
send \"vagrant\n\"
expect \"~]$\"
send \"cd /var/tmp\n\"
expect \"]$\"

send \"bash ./memory-use-rate-check.sh\n\"

expect \"tmp]$\"
exit 0
"
[vagrant@Jump tmp]$

3. Install local Windows PC Teraterm Macro

Work target: Local Windows PC Purpose: Kick expect-for-target.sh on Windows PC → Jump server

Expect_test> type jump-expect-kick.Instead of the ttl ← cat command, use the type command to display the contents.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
username = 'vagrant'
hostname = '192.168.18.20'
passwdfile = 'passwd.dat'
portnum = '22'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
call login
call exec
call exit
end
:login
    getpassword passwdfile username userpasswd
    msg = hostname
    strconcat msg ':portnum /ssh /auth=password /user='
    strconcat msg username
    strconcat msg ' /passwd='
    strconcat msg userpasswd
    strconcat msg inputstr
    connect msg
return
:exec
    wait '$ '
    msg = 'cd '
    strconcat msg '/var/tmp'
    sendln msg

    wait ']$ '
    msg = 'bash ./expect-for-target.sh'
    sendln msg
    pause 8
return
:exit
    wait '$ '
    msg = 'exit'
    sendln msg

4. Set the task schedule in Windows Task Manager.

Work target: Windows PC

    1. Run the following at the command prompt   compmgmt.msc ⇒ Computer management starts.
    1. task Scheduler ⇒Open the task scheduler library. Right-click to open Create Task.
    1. Task settings image.png

This completes.

-PS- I decided to join the Advent calendar in a hurry This is what I did recently in my drawer.

Recommended Posts

Verification of how to periodically execute a script on a Linux server on Windows
How to run Django on IIS on a Windows server
[Ubuntu] How to execute a shell script
How to live a decent life on 2017 Windows
How to access the contents of a Linux disk on a Mac (but read-only)
How to install Linux on a 32bit UEFI PC
Comparison of Windows Server and Free Linux to Commercial Linux
How to create a simple TCP server / client script
How to build a Python environment on amazon linux 2
How to use GitHub on a multi-person server without a password
A memo on how to easily prepare a Linux exercise environment
[Linux] Copy data from Linux to Windows with a shell script
How to mount a Windows 10 directory on Ubuntu-Server 20.04 on VMware Workstation 15
How to register a package on PyPI (as of September 2017)
[Linux] How to install a package on a server that does not have a network environment (standalone)
How to start a simple WEB server that can execute cgi of php and python
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
How to use Dataiku on Windows
Run a Linux server on GCP
How to install VMware-Tools on Linux
How to install pycrypto on Windows
How to deploy django-compressor on Windows
Create a Linux environment on Windows 10
How to display a specified line of a file or command result on Linux (sed, awk)
How to install music 21 on windows
How to display a specified column of files in Linux (awk)
I tried to create a server environment that runs on Windows 10
A note on how to check the connection to the license server port
How to output the output result of the Linux man command to a file
How to continue processing after returning a response on aiohttp Server
I will publish a shell script created to reduce the trouble of creating LiveUSB on Linux
How to install aws-session-manager-plugin on Manajro Linux
Execute a script from Jupyter to process
[Kivy] How to install Kivy on Windows [Python]
Build a simple WebDAV server on Linux
How to update php on Amazon linux 2
How to use Google Assistant on Windows 10
How to display emoji on Manjaro Linux
How to install packages on Alpine Linux
Build a Samba server on Arch Linux
How to install Anisble on Amazon Linux 2
How to install richzhang / colorization on Windows 10
How to update security on CentOS Linux 8
How to install php7.4 on Linux (Ubuntu)
Create a Linux virtual machine on Windows
How to test on a Django-authenticated page
How to find large files on Linux
How to run a Maya Python script
Build Linux on a Windows environment. Steps to install Laradock and migrate
Process and result of connecting to SQL Server on windows from Linux virtual environment using JDBC driver
How to execute Linux commands on Windows by people who do not know Linux (ubuntu) [Until checking the operation of R and python]
How to calculate the volatility of a brand
Various ways to execute .py files on Windows
A simple example of how to use ArgumentParser
One-liner to create a large number of test files at once on Linux
Install Windows 10 from a Linux server with PXE
Open a ZIP created on Windows in Linux
[Linux] How to read .bashrc of general user as root user on WSL2 Ubuntu20.04
Periodically execute Python Script on AWS Data Pipeline
LINUX: How to make arrow keys correspond to 2,4,6,8 on a notebook without a numeric keypad
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook