This article is used by students who are doing JS or something instead of memos. Don't expect the content.
#!/bin/bash
dname="/home/vagrant/workspace/itunes-rank-rss"
mkdir -p $dname
filename="${dname}/hourly-topsong-`date +'%Y%m%d%H%M'`.xml"
curl -s -H "User-Agent: CrawlBot;" -o $filename https://itunes.apple.com/jp/rss/topsongs/limit=10/xml
{#! / Bin / bash} is a description for executing a shell script in the bash shell in / bin / bash.
The second line shows the directory where the variable dname stores.
The third line uses the -p option to create a new directory if it doesn't exist, and it will work if it does.
The fourth line assigns the XML file with the specified name to the variable filename. $ {dname} is for treating dname as a variable rather than a string.
date +'%y%m%d%h%m'
This is a command that outputs a date and a character string to create a file name.
The fifth line uses the curl command.
A command to transfer data from the server or client side. The basic form is as follows.
```curl has various options. This time-s,-o,-I'm using three commands for h.
|option|meaning|How to use|
|:---:|:---:|:---:|
|-o|Output the execution result to a file|curl -o Output destination PATH URL|
|-s|Hide progress when outputting file|curl -s -o Output destination PATH URL|
|-H|Add to file header|curl -H string|
# 3. Run in a virtual environment
This time it will be done on Ubuntu, so start the virtual environment with iTerm2.
Where it started
1. Virtual Box
2.Vagrant
I use Ubuntu in a virtual environment that uses two software.
cd ~/vagrant/ubuntu
vagrant up
vagrant ssh
Move to the directory where Ubuntu is installed. vagrant up is a command to start Ubuntu installed on a virtual PC, and vagrant ssh connects to SSH with the Vagrant virtual machine set.
In order to run the shell script program, it is necessary to give the file permission to execute it, and this time I wrote it on the console so that it can be executed by all users.
#### **`chmod a+x niconico-ranking.sh`**
```sh
Since the program described this time is automatically executed, it is executed using cron.
Set up cron with the above contents. Write the following at the end of cron and specify it every 40 minutes to execute the program.
40 * * * * /home/vagrant/workspace/itunes-rank.sh
Check the contents of the directory with the ls command, and if there is a file, it is successful. Thank you for your hard work! !!
XML is translated as "extensible markup language" in Japanese. In XML, tags are used for the purpose of managing the enclosed character string as data in an easy-to-understand manner. XML is a language for describing data. You can make the data in the document easier to understand and exchange data. It is a language that conveys information to machines in an easy-to-understand and efficient manner.
Since it is a resident program (daemon) that is used as standard in many UNIX-based OSs, it starts the specified program periodically according to the schedule set by the user.
Recommended Posts