[LINUX] [Shell script] It's annoying to send the same content every week, so I tried to automate it! !! !!

Overview

We hold a cloudy study session every Saturday, but shortly before the start, "Paste the URL of zoom and send an email considering the text of the email." This work is troublesome. So let's create a fixed form and send it to slack automatically! !! It is the content.

■ Rough flow

  1. Create a file and grant execute permission
  2. Register and configure Incoming Webhook in slack
  3. Code creation (zoom URL + email text)
  4. Cron settings (job scheduling)

1. Create a file and grant execute permission

First, create a file to write the program. I created a file called Saturday_execute in the shellscript directory because it's a file to run on Saturday. (The path of ./shellscript/Saturday_execute is used in the cron settings.) Now let's grant permissions to Saturday_execute.

■ Display the file information in the directory

/shellscript


$ ls -l      #Display file information in the directory
-rw-r--r--1 File owner name File affiliation group File size Update date Saturday_execute(file name)

Usage and options of the ls command

Details of the displayed information --Three characters on the left: Owner's access rights --Central 3 characters: Access rights of owning group --Three characters on the right: Owner's access rights

-r: Readable --w: Writable --x: Executable ---: No access authority

Since the third character of the owner's access authority is "-", you can see that you do not have execute authority. Since you do not have execute permission, let's give execute permission.

■ Granting execution authority Use the chmod command to change permissions. How to use chmod and options

/shellscript


$ chmod u+x Saturday_execute    #chmod permission setting file path

Command explanation

symbol meaning
u Owner authority

As explained in "■ Displaying directory information", x was the execution authority.

It means "give execute permission to the owner of the Saturday_execute file".

2. Register and configure Incoming Webhook in slack

Search for Incoming Webhook from "+ button next to App" at the bottom left of slack. スクリーンショット 2020-07-26 11.53.54.png Click "Add Settings" スクリーンショット 2020-07-26 12.48.02.png Select the channel you want to be notified of and click Add Incoming Webhook Integration. スクリーンショット 2020-07-26 12.48.52.png Copy the Webhook URL for use by embedding it in the code. スクリーンショット 2020-07-26 12.49.01.png

This completes the settings! !!

3. Code creation (zoom URL + email text)

Set the URL setting of Incoming Webhook, send channel of slack, send name, icon, message and send with curl command.

/shellscript/Saturday_execute


#!/bin/bash

#################################################################################
#Email the URL for study session participation on Saturday morning
#Overview:Set the URL setting of Incoming Webhook, send channel of slack, send name, icon, message and send with curl command.
#Execution date:Every Saturday 9:55
#################################################################################

#Notify slack's "study session" channel(URL of Incoming Web Hooks)
WEBHOOKURL="URL of the copied Incoming Webhook"

###############################################################
#Creating a fixed text string
###############################################################
#slack send channel
CHANNEL="#study group"
#slack sender name
BOTNAME="Notice of study session information"
#slack icon
FACEICON=":snail:"  #I am using the slug icon. If you want to change the setting, "https"://www.webfx.com/tools/emoji-cheat-sheet/"use.
#Message content
MESSAGE="I will start a study session from now on.\n Please enter from the URL below.\n ■ Time: 10:00 to 17:00\n <zoom URL>"

curl -X POST --data-urlencode "payload={\"channel\": \"${CHANNEL}\", \"username\": \"${BOTNAME}\", \"icon_emoji\": \"${FACEICON}\", \"text\": \"${MESSAGE}\" }" ${WEBHOOKURL}

The code content is just storing the character string in a variable and sending it with the curl command! !!

・ If you don't understand the basic operation, see below! Basic commands for shell scripts ・ If you don't know how to use the curl command, see below! curl command options

4. Cron settings (job scheduling)

When you reach this point, all you have to do is set to "execute the specified program at the specified time"! !! ■ Job scheduling is a function that automatically executes a specified program at a specified time. The following two types

・ Cron --Automatic execution on a regular basis --Configure with crond and crontab commands ・ At --Automatic execution only once --Specify a script with standard input

This implementation will be notified weekly, so we will do it with cron. Now let's set the "/ shellscript / Saturday_execute" file to run at "9:55 every week".

python


$ crontab -e      #Start with e option
スクリーンショット 2020-07-26 14.13.17.png

Switch to insert mode with i and enter the following

crontab


# (Minutes)(Time)(Day)(Month)(曜Day)The path of the command to execute
55 9 * * sat bash ./shellscript/Saturday_execute    #Every Saturday at 9:55 "Saturday_execute "file execute

#After inputting, press the esc key:Save with wq

Press the exc key: Save with wq and you're done! !! !!

A notification will be sent to slack like this. スクリーンショット 2020-07-26 14.26.05.png

If you don't know the details of cron, please refer to the following.

Perform regular processing with cron I want to stop using numbers to specify the day of the week in the ron format

Recommended Posts

[Shell script] It's annoying to send the same content every week, so I tried to automate it! !! !!
I didn't understand the Resize of TensorFlow so I tried to summarize it visually.
I tried to scrape YouTube, but I can use the API, so don't do it.
I tried to expand the database so that it can be used with PES analysis software
I tried to automate the watering of the planter with Raspberry Pi
I tried to make it possible to automatically send an email just by double-clicking the [GAS / Python] icon
I tried to move the ball
I tried to estimate the interval.
When I tried to run Python, it was skipped to the Microsoft Store
Python -I tried to restore the dictionary comprehensive notation to its original form-
I tried to make a calculator with Tkinter so I will write it
Matching karaoke keys ~ I tried to put it on Laravel ~ <on the way>
The tree.plot_tree of scikit-learn was very easy and convenient, so I tried to summarize how to use it easily.
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I tried to summarize the umask command
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to publish my own module so that I can pip install it
When I tried to change the root password with ansible, I couldn't access it.
I tried to automate the 100 yen deposit of Rakuten horse racing (python / selenium)
[LIVE] I tried to deliver the sunrise and sunset times nationwide every day
I tried to rescue the data of the laptop by booting it on Ubuntu
I tried to automate the face hiding work of the coordination image for wear
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried to make OneHotEncoder, which is often used for data analysis, so that it can reach the itch.