<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Javed Shaikh</title>
    <description>The latest articles on DEV Community by Javed Shaikh (@shaikh).</description>
    <link>https://dev.to/shaikh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F499675%2F7e888334-5b0b-46ab-855a-0279f7cbc41e.jpeg</url>
      <title>DEV Community: Javed Shaikh</title>
      <link>https://dev.to/shaikh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shaikh"/>
    <language>en</language>
    <item>
      <title>What is fork() system call and how to fork using Python</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Tue, 26 Jan 2021 02:35:22 +0000</pubDate>
      <link>https://dev.to/shaikh/what-is-fork-system-call-and-how-to-fork-using-python-1ide</link>
      <guid>https://dev.to/shaikh/what-is-fork-system-call-and-how-to-fork-using-python-1ide</guid>
      <description>&lt;p&gt;Before going into fork, lets understand what is process.  A &lt;strong&gt;process&lt;/strong&gt; in computer term is a program being executed currently by a computer. Each process is unique and can be identified by its PID or process ID.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Note: All the examples and demo codes shown below were tried on Ubuntu 20.04 LTS and Python v3.8.5.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Visit my Github page for all the demo code snippets  &lt;a href="https://github.com/jaqsparow/fork-demos" rel="noopener noreferrer"&gt;https://github.com/jaqsparow/fork-demos&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  What we will learn in this post?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;What is fork 💡&lt;/li&gt;
&lt;li&gt;How to call fork in python 📗&lt;/li&gt;
&lt;li&gt;How to get process id or PID in Python 📙 &lt;/li&gt;
&lt;li&gt;How to identify parent and child process 📕&lt;/li&gt;
&lt;li&gt;Examples with code snippets 💻 &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Introduction : What is fork system call?
&lt;/h3&gt;

&lt;p&gt;Fork is one of the most important concept in Unix and Linux operating system. In short note, fork is nothing but cloning a process. That means fork will create a new process with exact copy of calling process. So when a program encounters a fork() system call, it will create another process with same copy of memory. So here comes the concept of parent and child process.&lt;/p&gt;

&lt;p&gt;The main or first process which calls fork and creates a new process is called &lt;strong&gt;parent process&lt;/strong&gt;. The new process created by fork is known as &lt;strong&gt;child process&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to identify parent and child process?
&lt;/h3&gt;

&lt;p&gt;Since both processes child and parent have exact same copy of memory, then comes the question how can we identify which of them is parent and which one is child. As I mentioned above, each process has unique ID known as process ID or PID which can be used to differentiate among processes. &lt;/p&gt;

&lt;p&gt;To identify parent and child process, we need to check the return code of fork system call. &lt;/p&gt;

&lt;h4&gt;
  
  
  Return code of fork()
&lt;/h4&gt;

&lt;p&gt;Return code of fork system call determines the parent or child process.&lt;br&gt;
When the parent process calls fork, fork returns ** PID of child process just created** to parent process and **0 **to child process. So basically if return code from fork call is zero, then its child process and if its a positive value, then it must be the parent process.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ZERO&lt;/strong&gt; If return code is 0, then it must be the child process&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A positive value&lt;/strong&gt; , If return code is positive value (or child's PID), then its parent process&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;Negative *&lt;/em&gt;, If return code is negative, then child process creation is failed and unsuccessful&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603476927418%2FIDJMNLLyf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603476927418%2FIDJMNLLyf.png" alt="fork.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  How to fork using Python?
&lt;/h3&gt;

&lt;p&gt;Python's &lt;strong&gt;os&lt;/strong&gt; module provides a function &lt;strong&gt;fork()&lt;/strong&gt; to create a child process. To know the PID of any process, use function &lt;strong&gt;getpid()&lt;/strong&gt; from &lt;strong&gt;os&lt;/strong&gt; module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets do some demos to understand whats going on&lt;/p&gt;

&lt;h4&gt;
  
  
  DEMO 1: To check process ID of any process
&lt;/h4&gt;

&lt;p&gt;In below example we are just checking how getpid() can be used to get the PID of current process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;demo1&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am the only process, My PID:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nf"&gt;demo1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603465824199%2F6lUSzeXhS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603465824199%2F6lUSzeXhS.png" alt="demo1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  DEMO 2: Create one  child process using fork()
&lt;/h4&gt;

&lt;p&gt;In below example we are printing process ID before and after fork() call. That means before fork, we have one process and after call we got another new process with total of 2 processes. &lt;/p&gt;

&lt;p&gt;Lets check the output of below snippets&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;demo2&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Before calling fork(),PID: &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;After calling fork(), PID: &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="nf"&gt;demo2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here goes output: -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;shaikh&lt;/span&gt;&lt;span class="nd"&gt;@ubuntu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Jupyter&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;fork&lt;/span&gt; &lt;span class="n"&gt;demos&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;python3&lt;/span&gt; &lt;span class="n"&gt;demo2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;
&lt;span class="n"&gt;Before&lt;/span&gt; &lt;span class="n"&gt;calling&lt;/span&gt; &lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="n"&gt;PID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="mi"&gt;6837&lt;/span&gt;
&lt;span class="n"&gt;After&lt;/span&gt; &lt;span class="n"&gt;calling&lt;/span&gt; &lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;PID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="mi"&gt;6837&lt;/span&gt;
&lt;span class="n"&gt;After&lt;/span&gt; &lt;span class="n"&gt;calling&lt;/span&gt; &lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;PID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="mi"&gt;6838&lt;/span&gt;
&lt;span class="n"&gt;shaikh&lt;/span&gt;&lt;span class="nd"&gt;@ubuntu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;Jupyter&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;fork&lt;/span&gt; &lt;span class="n"&gt;demos&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As displayed above, before fork() we only had one process with PID 6837 and after fork we have a new process with PID 6838. &lt;/p&gt;

&lt;h4&gt;
  
  
  Demo 3: To identify parent and child
&lt;/h4&gt;

&lt;p&gt;Lets see, how we can identify parent and child programmatically. As mentioned in the last section, that if return code from fork is zero, then its child process and if its a positive value then its parent process. Lets check the same here&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;demo3&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Before calling fork(),PID: &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;rc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;rc&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am child, PID: &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;rc&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;I am parent,PID:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Child process creation failed!!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;demo3&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@ubuntu:~/Jupyter/fork demos&lt;span class="nv"&gt;$ &lt;/span&gt;python3 demo3.py
Before calling fork&lt;span class="o"&gt;()&lt;/span&gt;,PID:  7316
I am parent,PID: 7316
I am child, PID:  7317
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets understand what happened above. Before fork we had only one process with PID 7316 and the moment it called fork(), we got another process. Each of those processes have different copy of return code &lt;strong&gt;rc&lt;/strong&gt;. The parent has rc with positive value (PID of child process) and child has the rc equals to &lt;strong&gt;zero&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Demo 4: Lets create two child process
&lt;/h4&gt;

&lt;p&gt;In below example, we are calling fork() twice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;demo4&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;#No fork, only one process
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Before any fork,  PID:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="c1"&gt;#First fork
&lt;/span&gt;    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;After first fork, PID:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="c1"&gt;#Second fork
&lt;/span&gt;    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;After second fork,PID:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="nf"&gt;demo4&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here goes the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@ubuntu:~/Jupyter/fork demos&lt;span class="nv"&gt;$ &lt;/span&gt;python3 demo4.py
Before any fork,  PID: 7471
After first fork, PID: 7471
After first fork, PID: 7472
After second fork,PID: 7471
After second fork,PID: 7473
After second fork,PID: 7472
After second fork,PID: 7474
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Before first fork there was only one process&lt;/li&gt;
&lt;li&gt;After first fork, total processes  are two &lt;/li&gt;
&lt;li&gt;After second call, total processes are four&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If we call fork 3 times, guess the number of processes that will be created in the comment section 🙂&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Demo 5: Lets do it for fun
&lt;/h4&gt;

&lt;p&gt;Below example will show that after fork call both parent and child will have different copies of variable &lt;strong&gt;num&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;demo5&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;rc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;rc&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getpid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;num: &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    
&lt;span class="nf"&gt;demo5&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And guess the output 🙂&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@ubuntu:~/Jupyter/fork demos&lt;span class="nv"&gt;$ &lt;/span&gt;python3 demo5.py
num:  7825
num:  0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in above code, only parent process can go inside &lt;strong&gt;if&lt;/strong&gt; statement because it has the positive response code which is PID of child process. Since &lt;strong&gt;rc&lt;/strong&gt; for child is &lt;strong&gt;ZERO&lt;/strong&gt;, it will still have the original copy of &lt;strong&gt;num&lt;/strong&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Hope this was fun and interesting learning 🙂. fork is very common way to create a child process in any Linux operating system. It is being used to create multiple processes and the most common use case is web server that forks a new process on each http request.&lt;/p&gt;

&lt;p&gt;Be careful when you use fork and make sure you have exited the process successfully after completion of a task else there will be high memory and cpu usage and may create memory leak situation which is called &lt;strong&gt;fork bomb&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All the demos are in my GitHub page.  &lt;a href="https://github.com/jaqsparow/fork-demos" rel="noopener noreferrer"&gt;Click here to visit&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/fork/fork.html" rel="noopener noreferrer"&gt;http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/fork/fork.html&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://people.cs.pitt.edu/~aus/cs449/ts-lecture14.pdf" rel="noopener noreferrer"&gt;http://people.cs.pitt.edu/~aus/cs449/ts-lecture14.pdf&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Similar posts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt; How to manage processes in Linux &lt;a href="https://shaikhu.com/how-to-manage-background-processes-in-linux" rel="noopener noreferrer"&gt;All about process&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;How to monitor CPU utilization in Linux  &lt;a href="https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux" rel="noopener noreferrer"&gt;cpu utilization&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;How to schedule jobs using crontab  &lt;a href="https://shaikhu.com/how-to-schedule-and-manage-tasks-using-crontab" rel="noopener noreferrer"&gt;How to use crontab&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>linux</category>
      <category>python</category>
      <category>javascript</category>
      <category>devops</category>
    </item>
    <item>
      <title>Files, Folders and Python</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Wed, 13 Jan 2021 04:09:28 +0000</pubDate>
      <link>https://dev.to/shaikh/files-folders-and-python-df4</link>
      <guid>https://dev.to/shaikh/files-folders-and-python-df4</guid>
      <description>&lt;p&gt;In this post we are going to see how we can use Python to find the size of any file and folder. We will check how many files we have inside a folder and how many of them are empty files and how to delete those empty files. We can also check how old a file is and when was the last time file was updated and accessed. We are going to use Python's powerful module &lt;strong&gt;os&lt;/strong&gt; for this. So lets start..&lt;/p&gt;

&lt;h3&gt;
  
  
  How to get current working directory and how to change it?
&lt;/h3&gt;

&lt;p&gt;First of all, lets import &lt;strong&gt;os&lt;/strong&gt; module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get the current working directory lets use &lt;strong&gt;getcwd()&lt;/strong&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getcwd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output:  C:\Users\jaqsp\Desktop\other&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  How to change working directory?
&lt;/h4&gt;

&lt;p&gt;Lets change the current working directory with &lt;strong&gt;chdir()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'C:&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Users&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;jaqsp&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Desktop'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets check the current directory once again&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getcwd&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output C:\Users\jaqsp\Desktop&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  How to get list of all files and folders inside a directory ?
&lt;/h3&gt;

&lt;p&gt;To get the list of files and folders inside a directory, we can use function &lt;strong&gt;scandir&lt;/strong&gt;. This function returns an object of &lt;strong&gt;os.DireEntry&lt;/strong&gt;. This object represents file path and other file attributes such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;name&lt;/strong&gt; : file or directory name&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;path&lt;/strong&gt; : full path of the file&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;is_dir()&lt;/strong&gt; : This function returns True if the entry is a directory&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;is_file()&lt;/strong&gt; : This function returns True if the entry is a file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So lets use &lt;strong&gt;scandir()&lt;/strong&gt; function as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;dirs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'C:&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Users&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;jaqsp&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Desktop&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;other'&lt;/span&gt;
&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scandir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DirEntry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;dirs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Directories: '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;dirs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Files :'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output : Directories: ['.ipynb_checkpoints', 'expense', 'git', 'qual-id']&lt;/p&gt;

&lt;p&gt;files : ['exp.json', 'files.ipynb', 'Google spreadsheet.ipynb', 'gspread.ipynb', 'my_screenshot.png', 'OS.ipynb']&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the above code, we have created two empty lists files and dirs and then used &lt;strong&gt;scandir()&lt;/strong&gt;  function with input path.&lt;br&gt;
We have used &lt;strong&gt;is_file()&lt;/strong&gt; and &lt;strong&gt;is_dir()&lt;/strong&gt; to identify if the entry returned is file or a directory. Finally we appended the list using append function&lt;/p&gt;
&lt;h3&gt;
  
  
  How to get file size ?
&lt;/h3&gt;

&lt;p&gt;There are two ways to get the file size i.e. using &lt;strong&gt;os.path.getsize()&lt;/strong&gt; function or using &lt;strong&gt;os.stat()&lt;/strong&gt; function. The size returned by each function is in bytes&lt;br&gt;
In below example we will get size of file &lt;em&gt;exp.json&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'exp.json'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output : 2313&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As shown the file size is 2313 bytes.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to get file create, update and access date, owner name and size of file
&lt;/h3&gt;

&lt;p&gt;We will use &lt;strong&gt;os.stat(file)&lt;/strong&gt; function to get all the details about a file. This function returns below along with other details. We are not mentioning all the return attributes. Please check  this link for all the attributes &lt;a href="https://docs.python.org/3/library/os.html"&gt;here&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;st_uid&lt;/strong&gt; : This attribute tells the user id of file owner&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;st_gid&lt;/strong&gt; : This is group id of file owner&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;st_size&lt;/strong&gt;: This is size of the file in bytes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;st_atime&lt;/strong&gt;: Last accessed time in no of seconds&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;st_ctime&lt;/strong&gt;: File creation time in seconds&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;st_mtime&lt;/strong&gt;: last update/modified time in seconds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now lets do an example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;stat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'exp.json'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;os.stat_result(st_mode=33206, st_ino=24206847997197434, st_dev=4237007564, st_nlink=1, st_uid=0, st_gid=0, st_size=2313, st_atime=1603293698, st_mtime=1602796599, st_ctime=1602796598)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As shown above file size is 2313 bytes and file creation/modification times are in seconds&lt;/p&gt;

&lt;p&gt;Lets convert the time in seconds to readable string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;
&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ctime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;st_ctime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output: 'Fri Oct 16 02:46:38 2020'&lt;br&gt;
We have used &lt;strong&gt;ctime&lt;/strong&gt; function to convert the time in seconds to string readable format&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  How to get folder size and empty files
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;os.path&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getsize&lt;/span&gt;
&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'C:&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Users&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;jaqsp&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Desktop&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;other'&lt;/span&gt;
&lt;span class="n"&gt;folder_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;empty_files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;all_files&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;dirs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;files&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;walk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paths&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;file_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getsize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;all_files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;file_size&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;empty_files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;folder_size&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;file_size&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'current folder size is'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;folder_size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'total files present: '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;all_files&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Total empty files: '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;empty_files&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;#Print all the empty files
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Below are empty files found'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;empty_files&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;empty_files&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;        
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;current folder size is 578231
total files present:  230
Total empty files:  4
Below are empty files found
C:\Users\jaqsp\Desktop\other\qual-id\qual_id__init__.py
C:\Users\jaqsp\Desktop\other\qual-id\test__init__.py
C:\Users\jaqsp\Desktop\other\qual-id\test\categories__init__.py
C:\Users\jaqsp\Desktop\other\qual-id\test\utils__init__.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we have used Python's &lt;strong&gt;walk(path)&lt;/strong&gt; function to list all the files inside the directory tree by walking either top-down or bottom-up&lt;br&gt;
Then we used &lt;strong&gt;join()&lt;/strong&gt; to join the path and file and return full path of the file and finally we use &lt;strong&gt;getsize()&lt;/strong&gt; function to retrieve size of the file. Folder size in above example is 578231 bytes (564KB)&lt;/p&gt;
&lt;h3&gt;
  
  
  How to remove a file and directory?
&lt;/h3&gt;

&lt;p&gt;Deleting or removing a file or directory in Python is super easy but make sure you want to delete it as it cannot be recovered once deleted.&lt;/p&gt;

&lt;p&gt;To remove or delete a file, Python's &lt;strong&gt;os&lt;/strong&gt; module provides function &lt;strong&gt;remove(path)&lt;/strong&gt;. If the input path is directory, it will through an exeception as this function is only to remove a file not a folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'abc.txt'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use &lt;strong&gt;rmdir(path)&lt;/strong&gt; function to remove or delete a directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rmdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'some_folder'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Hope this post will help to understand how we can use Python's &lt;strong&gt;OS&lt;/strong&gt; module to do daily activities related to files and directories. We learned how to get current directoy, how to change the directory and how to get different file attributes and also how we can delete a file and directory. Visit my GitHub page for code samples and don't forget to check my other posts.&lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt; &lt;a href="https://docs.python.org/3/library/os.html"&gt;https://docs.python.org/3/library/os.html&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Check my other posts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://shaikhu.com/how-to-develop-a-twitter-bot-using-python-and-nasa-api"&gt;https://shaikhu.com/how-to-develop-a-twitter-bot-using-python-and-nasa-api&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://shaikhu.com/how-to-keep-your-computer-awake-using-python"&gt;https://shaikhu.com/how-to-keep-your-computer-awake-using-python&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://shaikhu.com/how-to-schedule-and-manage-tasks-using-crontab"&gt;https://shaikhu.com/how-to-schedule-and-manage-tasks-using-crontab&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://shaikhu.com/how-to-access-google-sheets-using-python-and-gspread"&gt;https://shaikhu.com/how-to-access-google-sheets-using-python-and-gspread&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux"&gt;https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>codenewbie</category>
      <category>linux</category>
    </item>
    <item>
      <title>How to manage background processes in Linux</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Wed, 06 Jan 2021 03:36:51 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-manage-background-processes-in-linux-1d2m</link>
      <guid>https://dev.to/shaikh/how-to-manage-background-processes-in-linux-1d2m</guid>
      <description>&lt;p&gt;If you are new to Linux operating system and you start a command or process using terminal/session, you must have noticed that you need to wait till a process or command get finished before starting another command. This is because when you run a command using session or terminal, the process starts in the foreground by default. What should you do to run another command without opening a new terminal?&lt;br&gt;
In this post I am going to show how you can manage background and foreground process in Linux. &lt;/p&gt;

&lt;p&gt;But before that lets understand what is foreground process and what is background process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Foreground process&lt;/strong&gt; is the process or job that is currently running on the terminal. So there will be only one foreground process per terminal.You need to wait till the current foreground process finishes before starting a new foreground process.&lt;br&gt;
**Example: **Any command or process you start in the current session&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Background process&lt;/strong&gt; is the process or job running on the background and doesn't require interaction from the user. There can be more than one background process obviously.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; most common example is your web server.&lt;/p&gt;

&lt;p&gt;For this example I am going to start command VMSTAT 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;vmstat 5
procs &lt;span class="nt"&gt;-----------memory----------&lt;/span&gt; &lt;span class="nt"&gt;---swap--&lt;/span&gt; &lt;span class="nt"&gt;-----io----&lt;/span&gt; &lt;span class="nt"&gt;-system--&lt;/span&gt; &lt;span class="nt"&gt;------cpu-----&lt;/span&gt;
 r  b   swpd   free   buff  cache   si   so    bi    bo   &lt;span class="k"&gt;in   &lt;/span&gt;cs us sy &lt;span class="nb"&gt;id &lt;/span&gt;wa st
 0  0      0 3045156 167484 2449380    0    0    80    56  469 1138 19  5 76  0  0
 1  0      0 3044644 167492 2450152    0    0     0    92  299  604  2  1 98  0  0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;vmstat&lt;/strong&gt; is command that displays real time memory usage and cpu utillization. If you want to know more about it visit my previous post  &lt;a href="https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux-ckgcp9gex07g8pas1ees0hz7t"&gt;here&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;In the above command &lt;br&gt;
Things to note here is that above command will print CPU stats every five second on the terminal until you interrupt it. If you want to terminate it just press &lt;strong&gt;CTL + C&lt;/strong&gt; or if you want to pause or stop, press &lt;strong&gt;CTL + Z&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  bg: Move a job to background
&lt;/h3&gt;

&lt;p&gt;If you want to move a job that is already started to background so that you can access the terminal press &lt;strong&gt;CTL + Z ** and then **bg&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;lets start a job in the foreground using below command. Here we are writing CPU stats on a text file. As you can see , we cannot start a new command as this is running on the foreground.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets do &lt;strong&gt;CTL + Z&lt;/strong&gt; to pause this job and then do &lt;strong&gt;bg&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt
^Z
&lt;span class="o"&gt;[&lt;/span&gt;1]+  Stopped                 vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt
shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;bg&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;1]+ vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt &amp;amp;
shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the job running in the background and we got the terminal&lt;/p&gt;

&lt;h3&gt;
  
  
  How to start command and run it in the background
&lt;/h3&gt;

&lt;p&gt;To start a command and run it in the background use &lt;strong&gt;&amp;amp;&lt;/strong&gt; as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;command&lt;/span&gt; &amp;amp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sleep &lt;/span&gt;400 &amp;amp;
&lt;span class="o"&gt;[&lt;/span&gt;2] 11122
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  jobs : command to check the job status
&lt;/h3&gt;

&lt;p&gt;This command displays all the jobs running in the current terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;jobs&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;1]-  Running                 vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt &amp;amp;
&lt;span class="o"&gt;[&lt;/span&gt;2]+  Running                 &lt;span class="nb"&gt;sleep &lt;/span&gt;400 &amp;amp;
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here number withing bracket is [n] is job id or job number and &lt;strong&gt;+&lt;/strong&gt; indicates most recent command or job whereas &lt;strong&gt;-&lt;/strong&gt; indicates previous job. If you want to see the process id use &lt;strong&gt;-l&lt;/strong&gt; option&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;jobs&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;1]- 10216 Running                 vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt &amp;amp;
&lt;span class="o"&gt;[&lt;/span&gt;2]+ 11122 Running                 &lt;span class="nb"&gt;sleep &lt;/span&gt;400 &amp;amp;
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The number 10216 and 11122 are process id&lt;/p&gt;

&lt;p&gt;Different options for &lt;strong&gt;jobs&lt;/strong&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  -l :    lists process IDs in addition to the normal information
&lt;/h5&gt;

&lt;h5&gt;
  
  
  -n :    lists only processes that have changed status since the last
&lt;/h5&gt;

&lt;h5&gt;
  
  
  notification
&lt;/h5&gt;

&lt;h5&gt;
  
  
  -p :    lists process IDs only
&lt;/h5&gt;

&lt;h5&gt;
  
  
  -r :    restrict output to running jobs
&lt;/h5&gt;

&lt;h5&gt;
  
  
  -s :    restrict output to stopped jobs
&lt;/h5&gt;

&lt;h3&gt;
  
  
  kill %n: to kill a job with job id n
&lt;/h3&gt;

&lt;p&gt;Kill command is used to kill a job. Note that &lt;strong&gt;%&lt;/strong&gt; will be used to indicate job id or job number&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;haikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;jobs&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;1]-  Running                 vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt &amp;amp;
&lt;span class="o"&gt;[&lt;/span&gt;2]+  Running                 &lt;span class="nb"&gt;sleep &lt;/span&gt;400 &amp;amp;
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;kill&lt;/span&gt; %2
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;jobs&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;1]+  Running                 vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt &amp;amp;
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see now we have only one job running in the background and notice the &lt;strong&gt;+&lt;/strong&gt; sign which indicates the job id 1 becomes the most recent job 🙂&lt;/p&gt;

&lt;h3&gt;
  
  
  fg : command to move a job to foreground
&lt;/h3&gt;

&lt;p&gt;Use **fg **command to move a job to foreground. By default it will bring most recent background job to foreground&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;jobs&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;1]   Running                 vmstat 5 &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; vmstat.txt &amp;amp;
&lt;span class="o"&gt;[&lt;/span&gt;2]-  Running                 &lt;span class="nb"&gt;sleep &lt;/span&gt;400 &amp;amp;
&lt;span class="o"&gt;[&lt;/span&gt;3]+  Running                 &lt;span class="nb"&gt;sleep &lt;/span&gt;500 &amp;amp;
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;fg
sleep &lt;/span&gt;500
&lt;span class="o"&gt;[&lt;/span&gt;2]   Done                    &lt;span class="nb"&gt;sleep &lt;/span&gt;400
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have more than one jobs running in the background then use &lt;strong&gt;%n&lt;/strong&gt; to move a specific job to the foreground&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;fg&lt;/span&gt; %2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ps command to see all the processes
&lt;/h3&gt;

&lt;p&gt;Use &lt;strong&gt;ps&lt;/strong&gt; command to see active processes.&lt;br&gt;
Use below options&lt;/p&gt;

&lt;h5&gt;
  
  
  ps ax : to see all the process currently active in the system (Use my previous post to check the commands for real time process). It will be a very long list, so use less/more parameters
&lt;/h5&gt;

&lt;h5&gt;
  
  
  ps T: list all processes running on current terminal
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;ps T
    PID TTY      STAT   TIME COMMAND
   5786 pts/0    Ss     0:00 bash
  10216 pts/0    S      0:00 vmstat 5
  12983 pts/0    R+     0:00 ps T
shaikh@shaikhucom:~&lt;span class="nv"&gt;$ &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;So in this post we learned different commands to manage background and foreground process. We learned&lt;/p&gt;

&lt;h5&gt;
  
  
  - &lt;strong&gt;bg&lt;/strong&gt; and &lt;strong&gt;fg&lt;/strong&gt; to move a job to the background and foreground ##### respectively.
&lt;/h5&gt;

&lt;h5&gt;
  
  
  - &lt;strong&gt;jobs&lt;/strong&gt; command to list all the jobs active in the current terminal.
&lt;/h5&gt;

&lt;h5&gt;
  
  
  - &lt;strong&gt;kill&lt;/strong&gt; command to kill a job
&lt;/h5&gt;

&lt;h5&gt;
  
  
  - &lt;strong&gt;ps&lt;/strong&gt; command to see list of all the active and running processes
&lt;/h5&gt;

&lt;h5&gt;
  
  
  We also learned how to start a job in the background using &lt;strong&gt;&amp;amp;&lt;/strong&gt;. If you want to know real time memory and cpu usage and control all the processes running on your system please check my last post  &lt;a href="https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux-ckgcp9gex07g8pas1ees0hz7t"&gt;https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux-ckgcp9gex07g8pas1ees0hz7t&lt;/a&gt;
&lt;/h5&gt;

</description>
      <category>linux</category>
      <category>ubuntu</category>
      <category>javascript</category>
      <category>python</category>
    </item>
    <item>
      <title>How to monitor CPU utilization in Linux</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Fri, 01 Jan 2021 14:14:09 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-monitor-cpu-utilization-in-linux-5aeb</link>
      <guid>https://dev.to/shaikh/how-to-monitor-cpu-utilization-in-linux-5aeb</guid>
      <description>&lt;p&gt;Monitoring CPU utilization is one of the very important tasks of a back-end system engineer. CPU utilization is nothing but the total works or tasks being processed by your central processing unit or CPU. Its always a good practice for every developer to know the common utilities to monitor cpu utilization of the system they are working.&lt;/p&gt;

&lt;p&gt;In windows we use Task manager to check the statistical performance of CPU, memory ,disk network.. But what about Linux operating system. Lets check it out.&lt;/p&gt;

&lt;p&gt;We will go through these utilities one by one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;top&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;htop&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;nmon&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;vmstat&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  top
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;top&lt;/strong&gt; is one of the oldest command or utility to monitor system performance. It is a built in utility for any Linux operating system. It will display information like no of tasks, no of users, cpu utilization, statistical data about memory usage and list of active processes.&lt;/p&gt;

&lt;p&gt;Just input top and enter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;top
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here is the output of the &lt;strong&gt;top&lt;/strong&gt; command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;top - 11:23:33 up  3:11,  2 &lt;span class="nb"&gt;users&lt;/span&gt;,  load average: 0.86, 0.69, 1.01
Tasks: 317 total,   2 running, 315 sleeping,   0 stopped,   0 zombie
%Cpu&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt;: 34.6 us,  9.5 sy,  0.0 ni, 55.0 &lt;span class="nb"&gt;id&lt;/span&gt;,  0.0 wa,  0.0 hi,  0.8 si,  0.0 st
MiB Mem :   7814.9 total,    938.9 free,   2650.3 used,   4225.7 buff/cache
MiB Swap:   3906.0 total,   3906.0 free,      0.0 used.   3753.6 avail Mem 
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  7427 shaikh    20   0 9309320 483092 306628 S  65.7   6.0  24:12.65 chrome    
   1935 shaikh    20   0 4556040 288016 108556 S  17.6   3.6  10:12.54 gnome-shell  
   1655 shaikh    20   0  843820  76204  41576 S  10.8   1.0   5:44.07 Xorg 
   2831 shaikh    20   0 1574816 502964 363492 S   8.2   6.3   9:03.78 chrome  
   1572 shaikh     9 &lt;span class="nt"&gt;-11&lt;/span&gt; 2539652  19556  15264 S   7.2   0.2   2:08.94 pulseaudio
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now there are few quick commands available for top utility like press below keys for respective output on top command screen&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;k&lt;/strong&gt;  to kill a process. It will ask for process id or pid to kill&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;p&lt;/strong&gt;  to display processes based on cpu usage&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;m&lt;/strong&gt; to display list of processes based on memory usage&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;q&lt;/strong&gt; to quit&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  htop
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;htop&lt;/strong&gt; is another realtime cpu performance monitoring tool and is almost similar to &lt;strong&gt;top&lt;/strong&gt; command but with expanded screen. It is more user friendly and will give better viewing experience than top. You may need to install this utility using below command for ubuntu os&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;snap &lt;span class="nb"&gt;install &lt;/span&gt;htop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets take a look&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;htop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the output of &lt;strong&gt;htop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602874819950%2FDHBDHt0J_.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602874819950%2FDHBDHt0J_.jpeg" alt="rsz_1rsz_screenshot_from_2020-10-16_11-53-00.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  nmon
&lt;/h3&gt;

&lt;p&gt;nmon is system admin tool to monitor performance of a Linux operating system. This is developed by IBM's Nigel Griffiths and the name nmon is a short hand for &lt;strong&gt;N&lt;/strong&gt;igel's &lt;strong&gt;Mon&lt;/strong&gt;itor. It has nice colorful screen with many different statistical views.&lt;/p&gt;

&lt;p&gt;First lets install this utility using below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;nmon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;lets start it, type nmon and enter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;nmon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see below menu screen for nmon &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjsxi2dacbw6q8ocwx76h.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjsxi2dacbw6q8ocwx76h.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown above you have to select which realtime statistical data you want to see and use the keys accordingly&lt;br&gt;
Lets  type &lt;strong&gt;m&lt;/strong&gt; and &lt;strong&gt;c&lt;/strong&gt; to check realtime cpu and memory usage&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602876427577%2Ffg8IfdRkj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602876427577%2Ffg8IfdRkj.png" alt="Screenshot from 2020-10-16 12-26-22.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  vmstat
&lt;/h3&gt;

&lt;p&gt;This command can be used to display information about system processes, memory, swap, disk and cpu usage&lt;/p&gt;

&lt;p&gt;Lets type vmstat and enter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;vmstat
procs &lt;span class="nt"&gt;-----------memory----------&lt;/span&gt; &lt;span class="nt"&gt;---swap--&lt;/span&gt; &lt;span class="nt"&gt;-----io----&lt;/span&gt; &lt;span class="nt"&gt;-system--&lt;/span&gt; &lt;span class="nt"&gt;------cpu-----&lt;/span&gt;
 r  b   swpd   free   buff  cache   si   so    bi    bo   &lt;span class="k"&gt;in   &lt;/span&gt;cs us sy &lt;span class="nb"&gt;id &lt;/span&gt;wa st
 0  0  10240 645752 313692 3789356    0    0    55    66  493  109 18  5 76  0  0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to display vmstat for every 2 seconds, use below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;vmstat 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command will display system status every 2 seconds unless you interrupt it.&lt;br&gt;
Lets take another example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;vmstat 5 10
procs &lt;span class="nt"&gt;-----------memory----------&lt;/span&gt; &lt;span class="nt"&gt;---swap--&lt;/span&gt; &lt;span class="nt"&gt;-----io----&lt;/span&gt; &lt;span class="nt"&gt;-system--&lt;/span&gt; &lt;span class="nt"&gt;------cpu-----&lt;/span&gt;
 r  b   swpd   free   buff  cache   si   so    bi    bo   &lt;span class="k"&gt;in   &lt;/span&gt;cs us sy &lt;span class="nb"&gt;id &lt;/span&gt;wa st
 0  0  10240 512428 315012 3869748    0    0    52    65  487  154 18  5 77  0  0
 0  0  10240 512428 315020 3869588    0    0     0    45  396  738  2  1 98  0  0
 0  0  10240 481684 315028 3900856    0    0     0     7  726 1153  3  1 96  0  0
 0  0  10240 467320 315036 3914636    0    0     0    37  743 1730  6  2 92  0  0
 1  0  10240 486756 315052 3894072    0    0     0    90  694 1586  5  2 93  0  0
 0  0  10240 494316 315064 3887496    0    0     0    12  592 1264  3  1 95  0  0
 0  0  10240 500900 315072 3879552    0    0     0     9  623 1461  5  1 94  0  0
 0  0  10240 505200 315072 3874556    0    0     0    30  893 2205  8  2 90  0  0
 0  0  10240 474204 315080 3906004    0    0     0   156  971 2396  8  3 89  0  0
 0  0  10240 469416 315088 3910568    0    0     0    14  920 2261  8  2 89  0  0

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command will display system status every 5 seconds for 10 times &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Knowing and practicing above commands can help troubleshoot any process and performance related issue.&lt;br&gt;&lt;br&gt;
I know there are many more utilities out there  including iostat,sar,mpstat.. and what not 😊 but I have been using these four for most of the times to keep an eye on the system. Out of these, nmon and top are my favorite utility to check the system usage data and manage the processes accordingly. Let me know which utility you have been using to monitor system health.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>python</category>
      <category>linux</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>How to schedule and manage tasks using crontab</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Sat, 26 Dec 2020 03:19:32 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-schedule-and-manage-tasks-using-crontab-20dj</link>
      <guid>https://dev.to/shaikh/how-to-schedule-and-manage-tasks-using-crontab-20dj</guid>
      <description>&lt;p&gt;Before going into details about crontab, lets suppose you are running an online store and you want to send an email about latest deals on every Friday at 9AM MST. So how you can achieve this. Of course you can write your own job scheduler of n number of lines of codes if you have that much of time or you can simply use crontab available in all Unix and Linux operating system to schedule a task.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Crontab
&lt;/h3&gt;

&lt;p&gt;Crontab is short form of cron table. The cron is a utility available on all Linux and Unix operating system that runs a task or process at a given date and time. So  crontab is actually a table that contains script or commands along with date and time to be run at.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to view the crontab or cron table
&lt;/h3&gt;

&lt;p&gt;On Ubuntu you can use crontab -l to view the current table&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@shaikhu-com:~&lt;span class="nv"&gt;$ &lt;/span&gt;crontab &lt;span class="nt"&gt;-l&lt;/span&gt;
&lt;span class="c"&gt;# Edit this file to introduce tasks to be run by cron.&lt;/span&gt;
&lt;span class="c"&gt;# &lt;/span&gt;
&lt;span class="c"&gt;# Each task to run has to be defined through a single line&lt;/span&gt;
&lt;span class="c"&gt;# indicating with different fields when the task will be run&lt;/span&gt;
&lt;span class="c"&gt;# and what command to run for the task&lt;/span&gt;
&lt;span class="c"&gt;# &lt;/span&gt;
&lt;span class="c"&gt;# To define the time you can provide concrete values for&lt;/span&gt;
&lt;span class="c"&gt;# minute (m), hour (h), day of month (dom), month (mon),&lt;/span&gt;
&lt;span class="c"&gt;# and day of week (dow) or use '*' in these fields (for 'any').&lt;/span&gt;
&lt;span class="c"&gt;# &lt;/span&gt;
&lt;span class="c"&gt;# Notice that tasks will be started based on the cron's system&lt;/span&gt;
&lt;span class="c"&gt;# daemon's notion of time and timezones.&lt;/span&gt;
&lt;span class="c"&gt;# &lt;/span&gt;
&lt;span class="c"&gt;# Output of the crontab jobs (including errors) is sent through&lt;/span&gt;
&lt;span class="c"&gt;# email to the user the crontab file belongs to (unless redirected).&lt;/span&gt;
&lt;span class="c"&gt;# &lt;/span&gt;
&lt;span class="c"&gt;# For example, you can run a backup of all your user accounts&lt;/span&gt;
&lt;span class="c"&gt;# at 5 a.m every week with:&lt;/span&gt;
&lt;span class="c"&gt;# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/&lt;/span&gt;
&lt;span class="c"&gt;# &lt;/span&gt;
&lt;span class="c"&gt;# For more information see the manual pages of crontab(5) and cron(8)&lt;/span&gt;
&lt;span class="c"&gt;# &lt;/span&gt;
&lt;span class="c"&gt;# m h  dom mon dow   command&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use crontab -e to edit and manage cron table&lt;/p&gt;

&lt;h3&gt;
  
  
  How to set date and time of job run
&lt;/h3&gt;

&lt;p&gt;Below is the format of crontab that is supported by all Linux systems.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# * * * * * command to be executed&lt;/span&gt;
&lt;span class="c"&gt;# | | | | |&lt;/span&gt;
&lt;span class="c"&gt;# | | | | |&lt;/span&gt;
&lt;span class="c"&gt;# | | | | |&lt;/span&gt;
&lt;span class="c"&gt;# | | | | |_______________Day of the week (0 - 6)(Sunday to Saturday)&lt;/span&gt;
&lt;span class="c"&gt;# | | | |_______________Month (1 - 12)&lt;/span&gt;
&lt;span class="c"&gt;# | | |_______________Day of the Month(1 - 31)&lt;/span&gt;
&lt;span class="c"&gt;# | |_______________Hour(0 - 23)&lt;/span&gt;
&lt;span class="c"&gt;# |_______________Minute(0 - 59)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets get into some examples for better understanding&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt; Lets get into the first example we discussed at the beginning i.e.&lt;br&gt;
Run a job to send an email to all subscriber at 9AM every Friday&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;0  9  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  5  /usr/bin/python3 sendEmail.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt; In below example, we are running a job every 15 minutes( Note / operator)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;*&lt;/span&gt;/15  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  doSomething.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt; Here we are running a job every 1st and 5th hour ( Note the comma)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;*&lt;/span&gt;  1,5  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  doSomething.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 4:&lt;/strong&gt;  Below job runs on 1st January every year at 7AM&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;0  7  &lt;span class="k"&gt;*&lt;/span&gt;  1  &lt;span class="k"&gt;*&lt;/span&gt;  happyNewYear.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What if you want to run a job at random minutes every hour
&lt;/h3&gt;

&lt;p&gt;As of now we have seen how crontab can be used to schedule a job at a specific date and time. But what if we want to run a job at a random time or say random minute every hour. Well we can achieve this as well by writing a &lt;strong&gt;sleep&lt;/strong&gt; command as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;0  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="nv"&gt;$RANDOM&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="m"&gt;60&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;m&lt;span class="p"&gt;;&lt;/span&gt;sh test.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets understand what we did above. As per crontab rules above command set will be run at every hour. At the start of every hour, the cron will encounter the two commands and the first command is to sleep for random minutes. So this cron will delay for this random minutes and then it will execute the second command which is our job once it wakes up from sleep 😃&lt;/p&gt;

&lt;p&gt;If you want to do some more stuff than just sleep, we can write a shell script for the same instead of sleep.&lt;/p&gt;

&lt;p&gt;As shown below you can run a python script from shell script after delaying n no of minutes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Crontab:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;0  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  &lt;span class="k"&gt;*&lt;/span&gt;  sh test.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;test.sh&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/sh&lt;/span&gt;
&lt;span class="c"&gt;#test.sh&lt;/span&gt;

&lt;span class="nv"&gt;TIME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;RANDOM%60&lt;span class="k"&gt;))&lt;/span&gt;
&lt;span class="nb"&gt;sleep&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;TIME&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;m"&lt;/span&gt;
&lt;span class="c"&gt;#Do some stuff&lt;/span&gt;
&lt;span class="c"&gt;#Do some more stuff&lt;/span&gt;
/usr/bin/python3 /mybots/newsbot.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So basically you are running a python code from a shell script once after delaying RANDOM minutes and after doing some more code stuffs:)&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Cronjob is a very helpful utility table that is being used to schedule a task. Rules are very simple and easy to remember. You just have to follow the correct format to run a task or script. Hope this help you schedule your script/jobs :). &lt;/p&gt;

</description>
      <category>ubuntu</category>
      <category>python</category>
      <category>linux</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to access Google Sheets using Python and gspread</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Mon, 21 Dec 2020 03:44:40 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-access-google-sheets-using-python-and-gspread-4p2c</link>
      <guid>https://dev.to/shaikh/how-to-access-google-sheets-using-python-and-gspread-4p2c</guid>
      <description>&lt;p&gt;Hello everyone, In this post I am going to show how we can access and update Google spreadsheet using Python and an excellent library &lt;strong&gt;gspread&lt;/strong&gt;. With just few lines of codes we can create a new or use existing sheet and do the operation such as add, &lt;br&gt;
update or delete. Another cool thing is you can convert this spreadsheet into pandas dataframe using just one line. So lets get started..&lt;/p&gt;
&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Google developer account to create api credentials&lt;/li&gt;
&lt;li&gt;Google service account to access spreadsheet programmatically&lt;/li&gt;
&lt;li&gt;gspread - Python library&lt;/li&gt;
&lt;li&gt;oauth2client - Python library &lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Create Google service account
&lt;/h3&gt;

&lt;p&gt;**Step 1: **First you need to signup for a Google developer account if you have not already. Visit Google developer signup page at  &lt;a href="https://console.developers.google.com/"&gt;https://console.developers.google.com/&lt;/a&gt; to create your account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create service account&lt;/strong&gt;&lt;br&gt;
Since we will be accessing spreadsheet using programs, we need to create service account and credentials for it.  So login to your developer account and create new project by clicking at the top left corner new project icon.&lt;/p&gt;

&lt;p&gt;As shown below a new window will appear. Provide your project name and click create button&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RoYnltrU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603219492785/6aUnnXGrg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RoYnltrU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603219492785/6aUnnXGrg.png" alt="img1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now once project is created, it will take you to the project page (If not click on small bell icon on the top right and go to the project page). It should look like below screen print&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--35vEsh8N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603219644004/5_wjZgUML.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--35vEsh8N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603219644004/5_wjZgUML.png" alt="img2.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to enable APIs that we want to use. For this tutorial we need to enable api for Google drive and Google sheet. We need google drive api because your spreadsheet is going to be saved on your google drive. &lt;br&gt;
So click menu icon on top left corner and then go to '&lt;strong&gt;APIs and Services&lt;/strong&gt;' and then click &lt;strong&gt;Library&lt;/strong&gt; and it will take you to the API Library page like below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PYz1aAO3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603219949776/isSLOy9_J.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PYz1aAO3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603219949776/isSLOy9_J.png" alt="img3.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now search for Google Drive api and click enable &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QJ66uDOm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220002661/jDAa3rVja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QJ66uDOm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220002661/jDAa3rVja.png" alt="img4.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once enabled it will take you to Google drive api page. Now click &lt;strong&gt;create credentials&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OtKfkY_p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220070558/sYwIgZY7X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OtKfkY_p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220070558/sYwIgZY7X.png" alt="img5.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To create credentials follow the steps as shown in below screenshot and click "&lt;strong&gt;what credentials do I need?&lt;/strong&gt;"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZLv_S--f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220135393/UKnU5zR91.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZLv_S--f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220135393/UKnU5zR91.png" alt="img6.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the next page you need to provide service account name, service account id and role as editor since we want to do add and update operation on Google sheet programmatically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u8mr0HC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220378401/Ut_7thcLL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u8mr0HC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220378401/Ut_7thcLL.png" alt="img7.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you click continue in above step, a JSON file with authentication details will be created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1nuUdIFs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220458737/TEalm8X-6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1nuUdIFs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220458737/TEalm8X-6.png" alt="img8.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save this file in your system.&lt;/p&gt;

&lt;p&gt;At this point we have created the service account successfully. Now lets enable Google Sheet api using the same steps we used to enable Google drive api. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J6gewaJw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220567895/0J6YnwSHL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J6gewaJw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220567895/0J6YnwSHL.png" alt="img4a.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations!  we have successfully created our service account to access Google spreadsheet using Python. Now lets create a sample spreadsheet so that we can use it for our Python program to access. Lets go to  &lt;a href="https://docs.google.com/spreadsheets"&gt;https://docs.google.com/spreadsheets&lt;/a&gt;  and create a sample sheet. I have create a sheet named Student for this example tutorial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Bm5iupIk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220809508/wz5lZcvkX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Bm5iupIk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220809508/wz5lZcvkX.png" alt="img10.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use this sheet with our python code we need to share the file with Google api client email ID. Open a spreadsheet and click &lt;strong&gt;Share&lt;/strong&gt; button on top right corner of the spreadsheet. You can find the client email from the JSON file we just downloaded in the previous example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lAw5tjjF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220910385/WZ7K99SKh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lAw5tjjF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1603220910385/WZ7K99SKh.png" alt="img9.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Install Python libraries
&lt;/h3&gt;

&lt;p&gt;Install these libraries so that we can add and update spreadsheet using python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;gspread
pip &lt;span class="nb"&gt;install &lt;/span&gt;oauth2client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Though these two libraries are enough for us to for this tutorial. However we can install pandas to view our data using Pandas's Dataframe&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;pandas as pd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lets start coding
&lt;/h3&gt;

&lt;p&gt;At this point we have created Google account and then enabled Google drive api and Google sheet api and created credentials and saved in our local drive as JSON file. We also created a sample spreadsheet and shared the sheet with client email provided in the JSON file. We also installed required Python libraries in the above step. So now lets get started to code 🙂&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;gspread&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;oauth2client.service_account&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ServiceAccountCredentials&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With above code we imported the libraries we installed in the last step. Now before accessing the spreadsheet we need to authenticate using JSON file we downloaded&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Authenticate Google service account
&lt;/span&gt;&lt;span class="n"&gt;gp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gspread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;service_account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Testapp-9f185f872dd9.json'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to provide the correct path of JSON file. Since I have copied this file to my project directory, so just name is enough.&lt;/p&gt;

&lt;p&gt;Now lets open the spreadsheet 'Student' we created in one of the steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Open Google spreadsheet
&lt;/span&gt;&lt;span class="n"&gt;gsheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Student'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since spreadsheet is opened, now we need to select the worksheet we want to access using below line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Select worksheet
&lt;/span&gt;&lt;span class="n"&gt;wsheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;worksheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sheet1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As shown above we have selected 'Sheet1' which is the first worksheet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retrieve all records from a worksheet
&lt;/h3&gt;

&lt;p&gt;Now to retrieve the records or cell values we can use one of below functions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;get_all_values()&lt;/strong&gt; fetches all values from a worksheet as a list of lists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;get_all_records()&lt;/strong&gt; fetches all values from a worksheet as a list of dictionaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets try one by one on Jupyter Notebook&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;wsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_all_values&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output of above statement on Jupyter notebook. As shown all records are fetched as list of dictionaries&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="s"&gt;'Student'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Course'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Grade'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Score'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Data Science'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'480'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Amit'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Cyber Security'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'440'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Ronit'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Data Science'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'475'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Maya'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Business Analytics'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'481'&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly for get_all_values()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;wsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_all_records&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="s"&gt;'Student'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Course'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Data Science'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Grade'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Score'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;480&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'Student'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Amit'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Course'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Cyber Security'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Grade'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Score'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;440&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'Student'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Ronit'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Course'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Data Science'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Grade'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Score'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;475&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'Student'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Maya'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s"&gt;'Course'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Business Analytics'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s"&gt;'Grade'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s"&gt;'Score'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;481&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using Pandas DataFrame. First we extracted all values from worksheet, then set the columns for dataframe which is nothing but first list of all_rows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;all_rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_all_values&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;columns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;all_rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;all_rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;columns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the output of df&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;Course&lt;/span&gt;  &lt;span class="n"&gt;Grade&lt;/span&gt;   &lt;span class="n"&gt;Score&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt;   &lt;span class="n"&gt;John&lt;/span&gt;    &lt;span class="n"&gt;Data&lt;/span&gt; &lt;span class="n"&gt;Science&lt;/span&gt;    &lt;span class="n"&gt;A&lt;/span&gt;   &lt;span class="mi"&gt;480&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;   &lt;span class="n"&gt;Amit&lt;/span&gt;    &lt;span class="n"&gt;Cyber&lt;/span&gt; &lt;span class="n"&gt;Security&lt;/span&gt;  &lt;span class="n"&gt;B&lt;/span&gt;   &lt;span class="mi"&gt;440&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;   &lt;span class="n"&gt;Ronit&lt;/span&gt;   &lt;span class="n"&gt;Data&lt;/span&gt; &lt;span class="n"&gt;Science&lt;/span&gt;    &lt;span class="n"&gt;A&lt;/span&gt;   &lt;span class="mi"&gt;475&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;   &lt;span class="n"&gt;Maya&lt;/span&gt;    &lt;span class="n"&gt;Business&lt;/span&gt; &lt;span class="n"&gt;Analytics&lt;/span&gt;  &lt;span class="n"&gt;A&lt;/span&gt;   &lt;span class="mi"&gt;481&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Retrieve cell values from a worksheet
&lt;/h3&gt;

&lt;p&gt;To get a specific cell value use &lt;strong&gt;acell&lt;/strong&gt; function. Below snippet will print "John"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cell_A2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;acell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'A2'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cell_A2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get the value using coordinates use *&lt;em&gt;cell *&lt;/em&gt; function as shown below. This will also print "John"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;cell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating another worksheet
&lt;/h3&gt;

&lt;p&gt;To create another sheet on the same spreadsheet use &lt;strong&gt;add_worksheet&lt;/strong&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;another_sheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_worksheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"AnotherSheet"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cols&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create another sheet named "AnotherSheet" with 10 rows and 10 columns&lt;/p&gt;

&lt;h3&gt;
  
  
  Deleting a worksheet
&lt;/h3&gt;

&lt;p&gt;Use function **del_worksheet **to delete a worksheet. So below instruction will delete "AnotherSheet" we created just now in the last step&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;gsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;del_worksheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;another_sheet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Updating cell value
&lt;/h3&gt;

&lt;p&gt;To update cell values we can use **update **function like below&lt;br&gt;
Below operation will replace "John" with "David"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;wsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'A2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'David'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can perform same operation using coordinates&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;worksheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update_cell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'David'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Insert a new row
&lt;/h3&gt;

&lt;p&gt;With &lt;strong&gt;append_row&lt;/strong&gt;, we can append a new row at the end. As shown below, we are adding a new row for new student.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;wsheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append_row&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;"Gina"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Computer Science"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;447&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;As shown in above examples, &lt;strong&gt;gspread ** is an excellent and super easy to use library to access Google spreadsheet. Hopefully examples and steps shown above  will help you build your apps using Google spreadsheet. &lt;br&gt;
I have been using *&lt;em&gt;gspread *&lt;/em&gt; for one of my app to track my daily expenses using sms. I am going to post about this in my next story on how to build an expense tracker using **gspread&lt;/strong&gt;,&lt;strong&gt;Twilio **and **Flask&lt;/strong&gt; and then deploying it on Heroku 🙂&lt;/p&gt;

</description>
      <category>python</category>
      <category>codenewbie</category>
      <category>ubuntu</category>
      <category>showdev</category>
    </item>
    <item>
      <title>How to keep your computer awake using Python
</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Wed, 16 Dec 2020 16:09:47 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-keep-your-computer-awake-using-python-568p</link>
      <guid>https://dev.to/shaikh/how-to-keep-your-computer-awake-using-python-568p</guid>
      <description>&lt;p&gt;This sounds very funny but how can you let your team know that you are always online during work from home and doing your important stuffs 🙂. Here I will show how we can write a Python script with just few lines of code to keep our computer awake all the time. &lt;br&gt;
In this post, we are going to write a CLI script to keep our computer awake.&lt;/p&gt;
&lt;h3&gt;
  
  
  What this script will do?
&lt;/h3&gt;

&lt;p&gt;This script will take number of minutes as input and will keep right clicking every minute until it reaches the input time. We can add additional code to shutdown or restart the system once it reaches the countdown. It will also log the current system time in a text file and takes screenshot of the system before shutting down 🙂. &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;So what this CLI script will do?&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Takes number of minutes (stop_time) as input (No. of minutes computer will awake)
In a loop perform these after initiating a counter to 0 

&lt;ul&gt;
&lt;li&gt;Sleep for 60 seconds&lt;/li&gt;
&lt;li&gt;Perform mouse click to awake computer&lt;/li&gt;
&lt;li&gt;Increase a counter every minute from 0 to  stop_time&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Save the logs of system &lt;/li&gt;
&lt;li&gt;Take screenshot&lt;/li&gt;
&lt;li&gt;Shutdown, Restart or do nothing if no action is provided by user&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  What we are going to learn with this script
&lt;/h3&gt;

&lt;p&gt;Well with this script you can pretend to be working and will always be online 😁 but in real you are going to learn few interesting things like&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to do mouse operation such as right click  using Python 📙&lt;/li&gt;
&lt;li&gt;How to write a file in Python 📗&lt;/li&gt;
&lt;li&gt;How to check current system time &lt;/li&gt;
&lt;li&gt;How to take screenshot 📙&lt;/li&gt;
&lt;li&gt;How to shutdown or restart computer using Python 📕&lt;/li&gt;
&lt;li&gt;How to write CLI script in Python&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Start coding
&lt;/h3&gt;

&lt;p&gt;First of all lets import few libraries that we are going to use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;argparse&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pyautogui&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why we are using these libraries?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;argparse&lt;/strong&gt; : Since we are writing CLI script, we need to pass arguments using this library&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pyautogui&lt;/strong&gt; : For mouse operation and screenshot of the system&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;time&lt;/strong&gt; : to capture current system time&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;os&lt;/strong&gt; : to shutdown and restart the system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now lets write a new function named timer which will take two inputs i.e. input time or stop time and action to be performed. Please note that the input time is a integer variable which is number of minutes to keep our computer active&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stop_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we have defined a new function timer and initialized a counter to 0&lt;br&gt;
Next we are going to start a loop until counter reaches the stop_time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;stop_time&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above code will execute and end within a moment but we want to keep system active for stop_time. To achieve that we are going to sleep for a minute and then do right click for each iteration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;        &lt;span class="c1"&gt;#Lets sleep for 60 seconds only
&lt;/span&gt;        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;pyautogui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'right'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets understand what we did above.. &lt;br&gt;
&lt;strong&gt;sleep(n) **is a special python function to delay or pause further execution of codes for **n&lt;/strong&gt; no of seconds. Here we are delaying execution for 60 seconds in each iteration.&lt;br&gt;
 After 60 seconds we are doing mouse operation ('Right click') using Python library &lt;strong&gt;pyautogui&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next we are going to capture current system time using below instruction&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="c1"&gt;#Save current date and time
&lt;/span&gt;    &lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strftime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'%d-%b-%Y_%H-%M-%S'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;localtime&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We used &lt;strong&gt;strftime&lt;/strong&gt; function which convert local time to any readable format you want.  &lt;a href="https://strftime.org/"&gt;Visit this link for more details on the format&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now lets write the log in a text file named "shutdown_logs.txt". Please note we are using mode &lt;strong&gt;a+&lt;/strong&gt; so that we can append the log in the same file for each script run. You need to update the location of the text file accordingly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Create a text log when system shuts down
&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'C:&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Users&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;jaq&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Snapshot&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;shutdown_logs.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'a+'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we are going to write the log in the text file and close the file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   f.write('Shutting down at {}'.format(timestamp))
   f.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Take screenshot&lt;/strong&gt; before shutting down&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Take screenshot
&lt;/span&gt;&lt;span class="n"&gt;image_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'C:&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Users&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;jaq&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Snapshot&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;image_'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;'.jpeg'&lt;/span&gt;
&lt;span class="n"&gt;pyautogui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;screenshot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image_location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the last part we are going to issue shutdown or restart command based on user inputs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Shutdown system or Restart
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'s'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'S'&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"shutdown -s -f -t 0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'R'&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Shutdown -r -f -t 0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have noticed 'action' is nothing but the function parameter in the very first line of code. Depending on the action parameter, we are issuing command to shutdown or restart using &lt;strong&gt;system()&lt;/strong&gt; function of &lt;strong&gt;OS&lt;/strong&gt; module.&lt;/p&gt;

&lt;p&gt;With this , we are done with timer() function. Now we need to call it from our main function. So lets defined our function as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;argparse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ArgumentParser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_argument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"-t"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"--time"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;help&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Enter time in minutes"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_argument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"-a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"--action"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;help&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"want to shutdown?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets understand what we did above. We are using &lt;strong&gt;argparse&lt;/strong&gt; module to create our user friendly command line interface or CLI. In the first line we are creating **ArgumentParser **object and then in the next two lines we are adding two arguments for time and action input.&lt;/p&gt;

&lt;p&gt;Next we will be calling timer function which we defined earlier. We will call the timer function only if we have the input time argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse_args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it, we completed writing a CLI script in Python to keep our computer awake.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;If you want to take a look at complete code, its available in my Github repository here  &lt;a href="https://github.com/jaqsparow/keep-system-active"&gt;https://github.com/jaqsparow/keep-system-active&lt;/a&gt; &lt;br&gt;
Though it sounds funny that we wrote a script to keep our system active or pretend that we are always online :), however in this post, we learned some important Python modules to do mouse operation, to write a file, to take screenshot and to shutdown or restart computer all with just few lines of code. We have also covered beautiful python CLI module &lt;strong&gt;argparse&lt;/strong&gt; which is easy and efficient way of writing command line interface scripts.&lt;/p&gt;

&lt;h3&gt;
  
  
  complete code in Github
&lt;/h3&gt;

&lt;p&gt;Visit this Github page for the complete script  &lt;a href="https://github.com/jaqsparow/keep-system-active"&gt;click here&lt;/a&gt; &lt;/p&gt;

</description>
      <category>python</category>
      <category>codenewbie</category>
      <category>ubuntu</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to get email alert when your website is down using shell and Python?
</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Fri, 11 Dec 2020 03:21:12 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-get-email-alert-when-your-website-is-down-using-shell-and-python-1o62</link>
      <guid>https://dev.to/shaikh/how-to-get-email-alert-when-your-website-is-down-using-shell-and-python-1o62</guid>
      <description>&lt;p&gt;There are many ways to monitor and set alerts for your web server using third party apps but they don't come with free, be it Pingdom or Amazon's CloudWatch. In this post I am going to show  how we can write and setup our own alert tool with just few lines of code.&lt;br&gt;
However before going further into codes let us see what are the commands and utilities available in Linux to monitor web server health. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All the below commands are very basic and available on all Linux flavors. I am running these commands on Ubuntu 20.04 LTS for below demos&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  What we will learn in this article?
&lt;/h4&gt;

&lt;p&gt;📗 How to check if a website is up and running or down?&lt;br&gt;
📙 How to write a basic shell script?&lt;br&gt;
📗 How to call Python program from shell script?&lt;br&gt;
💡 How to send an email using Python?&lt;/p&gt;
&lt;h3&gt;
  
  
  How to check if web server is up and running?
&lt;/h3&gt;

&lt;p&gt;There are many commands and utilities available to monitor if your website or app is up and running fine. I have listed few of them. Lets checkout&lt;/p&gt;

&lt;p&gt;👉  &lt;strong&gt;curl&lt;/strong&gt; &lt;br&gt;
👉  &lt;strong&gt;wget&lt;/strong&gt;&lt;br&gt;
👉  &lt;strong&gt;ping&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Curl
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;curl&lt;/strong&gt; is a very powerful utility to send and receive data over many supported protocols such as HTTP,FTP, SMTP...&lt;br&gt;
We are going to use &lt;strong&gt;curl&lt;/strong&gt; with &lt;strong&gt;I&lt;/strong&gt; options to fetch the headers from a website. Based on the header we can identify if server is down or up. Lets try it&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;USAGE: *&lt;/em&gt; curl -I SERVER IP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@ubuntu:~/scripts&lt;span class="nv"&gt;$ &lt;/span&gt;curl &lt;span class="nt"&gt;-I&lt;/span&gt; &lt;span class="s2"&gt;"https://hashnode.com"&lt;/span&gt;
HTTP/2 200 
&lt;span class="nb"&gt;date&lt;/span&gt;: Thu, 29 Oct 2020 17:35:18 GMT
content-type: text/html&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nv"&gt;charset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;utf-8
cache-control: public, s-maxage&lt;span class="o"&gt;=&lt;/span&gt;1800, max-age&lt;span class="o"&gt;=&lt;/span&gt;0
etag: W/&lt;span class="s2"&gt;"4436a-AFOlSSR9igz5hRyOL+SdGTPJfxk"&lt;/span&gt;
vary: Accept-Encoding
x-frame-options: Deny
x-powered-by: Next.js
cf-cache-status: HIT
age: 1020
cf-request-id: 061706b33c0000051308a22000000001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As shown above &lt;strong&gt;curl -I&lt;/strong&gt; returns header and the very first line (HTTP/2 200 ) tells us that webserver for &lt;strong&gt;hashnode&lt;/strong&gt; is up and running. Note that server is up and running as long as the response code is 200,301,302,308. In above example response code is 200 , so its up.&lt;/p&gt;

&lt;h4&gt;
  
  
  wget
&lt;/h4&gt;

&lt;p&gt;This is another utility in linux used as network down loader. We will use option &lt;strong&gt;-S&lt;/strong&gt; to get the headers from a website.&lt;br&gt;
&lt;strong&gt;USAGE:&lt;/strong&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;wget -s SERVER : This will fetch and download the headers and pages of a website&lt;/p&gt;

&lt;p&gt;wget -S --spider SERVER :This will work exact similar way as above except that it will behave as web spider and  wont download the pages.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@ubuntu:~/scripts&lt;span class="nv"&gt;$ &lt;/span&gt;wget &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nt"&gt;--spider&lt;/span&gt; http://shaikhu.com
URL transformed to HTTPS due to an HSTS policy
Spider mode enabled. Check &lt;span class="k"&gt;if &lt;/span&gt;remote file exists.
&lt;span class="nt"&gt;--2020-10-29&lt;/span&gt; 10:47:55--  https://shaikhu.com/
Resolving shaikhu.com &lt;span class="o"&gt;(&lt;/span&gt;shaikhu.com&lt;span class="o"&gt;)&lt;/span&gt;... 192.241.200.144
Connecting to shaikhu.com &lt;span class="o"&gt;(&lt;/span&gt;shaikhu.com&lt;span class="o"&gt;)&lt;/span&gt;|192.241.200.144|:443... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 302 Found
  Alt-Svc: h3-29&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;":443"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nv"&gt;ma&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2592000
  Content-Length: 206
  Content-Type: text/plain&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nv"&gt;charset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;utf-8
  Date: Thu, 29 Oct 2020 17:47:55 GMT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As shown above the returned response code is &lt;strong&gt;302&lt;/strong&gt; for redirection, so our server is up.&lt;/p&gt;

&lt;h4&gt;
  
  
  Ping
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Ping&lt;/strong&gt; is most common utility to check the web server's health. It sends ICMP ECHO_REQUEST to network host to get the up information. We are going to use &lt;strong&gt;-C&lt;/strong&gt; count option to send n number of request packets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;USAGE&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ping -c COUNT SERVER&lt;br&gt;
Above command will send COUNT no of request packets to SERVER&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@ubuntu:~/scripts&lt;span class="nv"&gt;$ &lt;/span&gt;ping &lt;span class="nt"&gt;-c&lt;/span&gt; 3 shaikhu.com
PING hashnode.network &lt;span class="o"&gt;(&lt;/span&gt;192.241.200.144&lt;span class="o"&gt;)&lt;/span&gt; 56&lt;span class="o"&gt;(&lt;/span&gt;84&lt;span class="o"&gt;)&lt;/span&gt; bytes of data.
64 bytes from 192.241.200.144 &lt;span class="o"&gt;(&lt;/span&gt;192.241.200.144&lt;span class="o"&gt;)&lt;/span&gt;: &lt;span class="nv"&gt;icmp_seq&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 &lt;span class="nv"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;55 &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;38.2 ms
64 bytes from 192.241.200.144 &lt;span class="o"&gt;(&lt;/span&gt;192.241.200.144&lt;span class="o"&gt;)&lt;/span&gt;: &lt;span class="nv"&gt;icmp_seq&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2 &lt;span class="nv"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;55 &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;34.0 ms
64 bytes from 192.241.200.144 &lt;span class="o"&gt;(&lt;/span&gt;192.241.200.144&lt;span class="o"&gt;)&lt;/span&gt;: &lt;span class="nv"&gt;icmp_seq&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3 &lt;span class="nv"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;55 &lt;span class="nb"&gt;time&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;36.0 ms

&lt;span class="nt"&gt;---&lt;/span&gt; hashnode.network ping statistics &lt;span class="nt"&gt;---&lt;/span&gt;
3 packets transmitted, 3 received, 0% packet loss, &lt;span class="nb"&gt;time &lt;/span&gt;2003ms
rtt min/avg/max/mdev &lt;span class="o"&gt;=&lt;/span&gt; 33.954/36.039/38.178/1.724 ms
shaikh@ubuntu:~/scripts&lt;span class="nv"&gt;$ &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As shown above we sent 3 packets and received 3 packets as well, so that means our website is up and running&lt;/p&gt;

&lt;h3&gt;
  
  
  How to write a basic shell script?
&lt;/h3&gt;

&lt;p&gt;Now that we know which commands and utility to use to know the status of our website, so lets get started. We are going to issue the command &lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1 : Create a new file for shell script
&lt;/h4&gt;

&lt;p&gt;Lets create a new file named &lt;em&gt;alert.sh&lt;/em&gt; and save it to your home folder. We can use any of the above command to know the status of a website. I am going to use &lt;strong&gt;wget&lt;/strong&gt;'s web spider option to get the details. However you can use any of them.&lt;/p&gt;

&lt;p&gt;Before writing any script, note the exact location of &lt;em&gt;bash&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;shaikh@ubuntu:~/scripts&lt;span class="nv"&gt;$ &lt;/span&gt;which bash
/usr/bin/bash 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets start writing shell script &lt;em&gt;alert.sh&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/bash&lt;/span&gt;
&lt;span class="nv"&gt;MYHOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://YOUR WEBSITE ADDRESS"&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;wget &lt;span class="nt"&gt;--spider&lt;/span&gt; &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nv"&gt;$MYHOST&lt;/span&gt; 2&amp;gt;&amp;amp;1 | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-w&lt;/span&gt; &lt;span class="s2"&gt;"200&lt;/span&gt;&lt;span class="se"&gt;\|&lt;/span&gt;&lt;span class="s2"&gt;301&lt;/span&gt;&lt;span class="se"&gt;\|&lt;/span&gt;&lt;span class="s2"&gt;302&lt;/span&gt;&lt;span class="se"&gt;\|&lt;/span&gt;&lt;span class="s2"&gt;308"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null
&lt;span class="k"&gt;then
&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"server is up"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="c"&gt;#Server is down"&lt;/span&gt;
/usr/bin/python3 send_alert.py
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a very simple script to monitor your web server and send an email notification if its down. Lets understand what we did line by line.&lt;/p&gt;

&lt;p&gt;In the very first line we are providing location of &lt;em&gt;bash&lt;/em&gt; which was found in the previous step above&lt;br&gt;
then we have saved our website address in a variable named MYHOST&lt;br&gt;
From line number 3 we are executing the &lt;em&gt;wget&lt;/em&gt; command in an &lt;em&gt;if else&lt;/em&gt; statement. If the return code from wget is other than 200,301,302 and 308 then control will move to else block considering server is down or unreachable.&lt;br&gt;
In this step you can change the command to &lt;em&gt;ping&lt;/em&gt; or &lt;em&gt;curl&lt;/em&gt; as you like&lt;/p&gt;

&lt;p&gt;In the &lt;em&gt;else&lt;/em&gt; block we are executing a Python file named &lt;em&gt;send_alert.py&lt;/em&gt; to send an email notification. You can ignore this step if you are sending email using MAILX of linux package within shell script. However I dont want to install any third party library and want to use existing Python library to send the email.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to send an email using Python?
&lt;/h3&gt;

&lt;p&gt;Writing an email using Python is very simple. No third party library is needed to send an email. Here we are using Python's inbuilt library &lt;em&gt;smtplib&lt;/em&gt; to send an email. &lt;br&gt;
As shown rest of the code is pretty simple. We have defined &lt;strong&gt;FROM_EMAIL&lt;/strong&gt;,&lt;strong&gt;Password&lt;/strong&gt;, &lt;strong&gt;TO_EMAIL&lt;/strong&gt;,&lt;strong&gt;header&lt;/strong&gt; and finally &lt;strong&gt;body&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets see what our &lt;em&gt;send_alert&lt;/em&gt; looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;smtplib&lt;/span&gt;

&lt;span class="n"&gt;FROM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;FROM EMAIL ADDRESS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;PSWD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;YOUR PASSWORD&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="n"&gt;TO&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;TO EMAIL ADDRESS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;#Subject and header
&lt;/span&gt;&lt;span class="n"&gt;SUB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Subject: Alert for shaikhu.com &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;To:&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;TO&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;From: &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;FROM&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;SUB&lt;/span&gt;

&lt;span class="c1"&gt;#Message
&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
MAY DAY! MAY DAY!! MAY DAY!!!
Your webserver for shaikhu.com is DOWN.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;
&lt;span class="c1"&gt;#Define function to send email
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sendalert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;smtpObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;smtplib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SMTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;smtp.mail.yahoo.com&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;587&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;smtpObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ehlo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;smtpObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;starttls&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;smtpObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FROM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PSWD&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;smtpObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FROM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;TO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;smtpObj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;quit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;sendalert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations!!🥳. We have just wrote our own script to monitor our server and send us an email notification if it is down. Now all we have to do is put this script on a scheduler or cron table so that the script will run every &lt;strong&gt;n&lt;/strong&gt; no of minutes and keep monitoring our website.&lt;/p&gt;

&lt;h4&gt;
  
  
  Add the job to scheduler or crontab
&lt;/h4&gt;

&lt;p&gt;If you want to know how to setup job scheduler using crontab then please do check my article  &lt;a href="https://shaikhu.com/how-to-schedule-and-manage-tasks-using-crontab" rel="noopener noreferrer"&gt;https://shaikhu.com/how-to-schedule-and-manage-tasks-using-crontab&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*/5  *  *  *  *  sh alert.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As per above cron table, our alert.sh will be executed every 5 minutes. That means alert.sh will check our website health every 5 minutes and will send me an email if the website is down. &lt;/p&gt;

&lt;p&gt;This is how the email from my script looks like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603999870746%2FvbeSw5dUa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1603999870746%2FvbeSw5dUa.png" alt="email.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Though there are many websites are providing website alert service but not all of them are free. As seen in this article with just few lines of code, you can build your own alert app that will keep monitoring your website every moment and will send an email notifications when its down.&lt;/p&gt;

&lt;p&gt;We have learned some important commands and codes such as how to check website is down or up and how to send email using Python and also how to write a basic shell script.&lt;/p&gt;

&lt;p&gt;Hope this tutorial helped you learn something new today. Let me know your thoughts in the comment section 😃.&lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt; &lt;a href="https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail" rel="noopener noreferrer"&gt;https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>codenewbie</category>
      <category>linux</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>How to auto change desktop wallpaper every minute using Python</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Sat, 07 Nov 2020 16:06:32 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-auto-change-desktop-wallpaper-every-minute-using-python-46hi</link>
      <guid>https://dev.to/shaikh/how-to-auto-change-desktop-wallpaper-every-minute-using-python-46hi</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This post was originally posted on &lt;a href="https://shaikh.com" rel="noopener noreferrer"&gt;https://shaikh.com&lt;/a&gt;&lt;br&gt;
This post has been updated to include changes to support Linux operating system as well. So you can use this code to change desktop background in both linux and windows computers 🙂.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this fun tutorial we are going to download a random wallpaper from **pexels **and set it to our desktop background every minute using Python. While it sounds like fun project, but we are going to learn some important concepts like how to make http request, how to download a file and save it to your local machine and most importantly how to set the wallpaper using Windows's system function.&lt;/p&gt;

&lt;p&gt;Visit my GitHub page for complete codes at  &lt;a href="https://github.com/jaqsparow/change-desktop-wallpaper" rel="noopener noreferrer"&gt;https://github.com/jaqsparow/change-desktop-wallpaper&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: All these code snippets are tested on windows 10 and Ubuntu 20.04 LTS, with Python v3.9.0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What we will build?
&lt;/h3&gt;

&lt;p&gt;We are going to build a CLI app using Python to change desktop wallpaper every given number of minutes. Every wallpaper downloaded from internet will be unique and our app will change the wallpaper based on the time we set.&lt;/p&gt;

&lt;h4&gt;
  
  
  What we will learn in this post?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;How to generate random number?  📗&lt;/li&gt;
&lt;li&gt;How to make http request using Python? 📕&lt;/li&gt;
&lt;li&gt;How to download an image to your local machine?📙&lt;/li&gt;
&lt;li&gt;How to set wallpaper using Python 💡&lt;/li&gt;
&lt;li&gt;How to call windows's C++ system function from Python💡&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Get API key to download wallpaper
&lt;/h3&gt;

&lt;p&gt;First of all, we need an api key to make HTTP request and download image. For this tutorial, I am going to use &lt;strong&gt;pexels&lt;/strong&gt;. Follow these steps to get the api key&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pexels.com" rel="noopener noreferrer"&gt;&lt;br&gt;
  &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.pexels.com%2Flib%2Fapi%2Fpexels.png"&gt;&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 1: Register for a free account
&lt;/h4&gt;

&lt;p&gt;Visit this link and then click "Join" link. Provide your name and email details in the text provided and create an account&lt;br&gt;
 &lt;a href="https://www.pexels.com/join-consumer/" rel="noopener noreferrer"&gt;https://www.pexels.com/join-consumer/&lt;/a&gt; &lt;/p&gt;
&lt;h4&gt;
  
  
  Step 2: Get started
&lt;/h4&gt;

&lt;p&gt;Once you registered and confirmed your email visit   &lt;a href="https://www.pexels.com/api/" rel="noopener noreferrer"&gt;https://www.pexels.com/api/&lt;/a&gt; and click "Get Started" and it will take you to new page for applications. Click new application and provide your application details. &lt;/p&gt;

&lt;p&gt;After above step you should be provided with an &lt;strong&gt;api key&lt;/strong&gt; which we are going to use in the next step. So save this api key somewhere in your machine.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note - Do not abuse the &lt;em&gt;Pexels&lt;/em&gt; api. They are providing 200 requests per hour with a free account. So respect 👏&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  Install requests module
&lt;/h4&gt;

&lt;p&gt;To make an http request, we need a module &lt;strong&gt;requests&lt;/strong&gt; . so lets install this module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have everything setup and lets go to next step to start coding 💻.&lt;/p&gt;

&lt;h3&gt;
  
  
  Start coding..
&lt;/h3&gt;

&lt;p&gt;First we need to import all the modules we need to make this app. So lets get started.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;argparse&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ctypes&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;platform&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Why we need these modules
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;*&lt;em&gt;argparse: *&lt;/em&gt; Since this is a CLI app, we need to pass arguments to our app using this module&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;os: *&lt;/em&gt; We need this module to find the current working directory and full path of our wallpaper&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;time: *&lt;/em&gt; This module will be used to call our function every given minutes and also to use sleep() method&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;random:&lt;/strong&gt; To generate random number , so we can make random request url every time&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;requests: *&lt;/em&gt; We are using this module to make http request and to download the wallpaper&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ctypes: ** This module is to call foreign function in Python. It provides C compatible data types and allows to call DLL and shared libraries. We need this to call **SystemParametersInfoW&lt;/strong&gt; to change our wallpaper&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;platform: *&lt;/em&gt; We are using this inbuilt module to know the operating system where this code will run. Based on that we will call system function to change the wallpaper&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Generate a random number
&lt;/h4&gt;

&lt;p&gt;Lets create a function named &lt;strong&gt;get_wallpaper()&lt;/strong&gt; and generate a random number inside this function, which we will use to make random http request URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_wallpaper&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;#Random number
&lt;/span&gt;    &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Make http request
&lt;/h4&gt;

&lt;p&gt;Now lets save our api key inside the function as shown below. Replace your api keys in the field &lt;em&gt;&lt;/em&gt; and also set the &lt;em&gt;url for pexel request&lt;/em&gt; as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="c1"&gt;#Set your api key here
&lt;/span&gt;    &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Authorization&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;Your api key goes here&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;#Search query
&lt;/span&gt;    &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;flower&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="c1"&gt;#URL for PEXELS
&lt;/span&gt;    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://api.pexels.com/v1/search?per_page=1&amp;amp;page=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;&amp;amp;query=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With above codes, we are creating a random URL with &lt;strong&gt;page&lt;/strong&gt; parameter and query =&lt;strong&gt;flower&lt;/strong&gt; 🙂. Change the query depending on what type of wallpaper you want to download like nature, mountain, airplane or anything you want. I am downloading wallpaper with query &lt;strong&gt;flower&lt;/strong&gt;&lt;br&gt;
So we have the url, now lets make the http request to that url with &lt;em&gt;get method&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code, we are making http request to &lt;strong&gt;Pexels&lt;/strong&gt; with api key in the headers. Also note that we have saved the response in &lt;strong&gt;res&lt;/strong&gt; variable which we are going to use in our next step.&lt;br&gt;
Now its time to download the image. Lets do that using below code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="c1"&gt;#Get the url of the image from the response
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;img_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;photos&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;src&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;original&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="c1"&gt;#Make request to get the image
&lt;/span&gt;        &lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;#Write and save the imgae locally with name temp.jpg
&lt;/span&gt;        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;temp.jpg&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;wb&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;error in making http request&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets understand the above code. In the very first line we are checking the status code from our http request in the last step. Please note that for every http request, we will get the response with status_code. If the status code is 200, that means the request was successful and a good response object should be returned. &lt;br&gt;
We are converting the response content into json object so that we can use it parse the image url. &lt;/p&gt;

&lt;p&gt;In the second line we got the image_url after parsing the JSON response object.&lt;br&gt;
We are making another http request to download the image and finally we are saving the image  in our local system using &lt;strong&gt;write&lt;/strong&gt; method. Please note that we are saving the file using &lt;strong&gt;wb&lt;/strong&gt; mode so that a new file will be downloaded and replaced the old one. We are saving the wallpaper as "temp.jpg"&lt;/p&gt;

&lt;p&gt;Finally we are printing 'error message' if the status code returned is not &lt;strong&gt;200&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Set wallpaper using Python
&lt;/h4&gt;

&lt;p&gt;In this section we are going to create a new function &lt;strong&gt;set_wallpaper()&lt;/strong&gt; as follow&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_wallpaper&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;get_wallpaper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;#Check the operating system
&lt;/span&gt;    &lt;span class="n"&gt;system_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;system&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;''&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;system_name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;linux&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getcwd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/temp.jpg&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gsettings set org.gnome.desktop.background picture-uri file:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;system&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;system_name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;windows&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getcwd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;temp.jpg&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="n"&gt;ctypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;windll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user32&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SystemParametersInfoW&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets take a look what we did above. In the first line inside &lt;strong&gt;set_wallpaper()&lt;/strong&gt; we are calling the function &lt;em&gt;get_wallpaper()&lt;/em&gt; to download the image and save it to &lt;em&gt;temp.jpg&lt;/em&gt; file. In the next line, we are assigning &lt;em&gt;path&lt;/em&gt; variable with the complete path of the image that we just downloaded.&lt;/p&gt;

&lt;p&gt;Please note we are using &lt;strong&gt;platform.system()&lt;/strong&gt; function to identify the operating system and based on system we are running respecting command to change the desktop wallpaper.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to call Windows' system function?
&lt;/h4&gt;

&lt;p&gt;The next line shown below is interesting. With the help of &lt;strong&gt;ctypes&lt;/strong&gt; module, we are calling Windows's system C++ function &lt;em&gt;SystemParametersInfoW()&lt;/em&gt; to change the wallpaper. Lets see different parameters of this function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;ctypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;windll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user32&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SystemParametersInfoW&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BOOL SystemParametersInfoW(
  UINT  uiAction,
  UINT  uiParam,
  PVOID pvParam,
  UINT  fWinIni
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;uiAction: ** this is systemwide parameter (such as desktop/icon/menu..)to be set or retrieved.
&amp;gt; We are using SPI_SETDESKWALLPAPER or 0x0014 to set this parameter. Please note 0x0014 is nothing but **20&lt;/strong&gt; in decimal which are sending in above code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;uiParam:&lt;/strong&gt; This is second parameter which depends system parameter of windows. We are sending &lt;strong&gt;0&lt;/strong&gt; as for most of the request. as suggested by the usage document.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pvParam:&lt;/strong&gt; This parameter can be used to send the argument for action set in the first parameter. With this we are sending full path of the image&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;fWinIni:&lt;/strong&gt; This parameter can be used to set or update user profile or broadcast the change to all the top level windows. In this case we don't want to broadcast anything , so we are sending &lt;strong&gt;0&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Return code
&lt;/h4&gt;

&lt;p&gt;If the above function execution succeeds, the return value will be nonzero else it will be zero&lt;/p&gt;

&lt;p&gt;With this we are done with the coding to change and set the wallpaper. Now its time to call this function every minute&lt;/p&gt;

&lt;h4&gt;
  
  
  Call function every minute to change the wallpaper
&lt;/h4&gt;

&lt;p&gt;Here we will be calling from the main function based on the user input. Since this is CLI app, we are going to ask user for what would be the frequency to change the wallpaper? every minute, or every 5 minutes or any other value?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;argparse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ArgumentParser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_argument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-t&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--time&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;help&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Enter time in minutes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse_args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;minute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minute&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;set_wallpaper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above code we are setting up the entry point of our app. First two lines are to setup CLI arguments where we are setting up input argument &lt;strong&gt;-t&lt;/strong&gt; which will take number minutes we will be calling &lt;em&gt;set_wallpaper()&lt;/em&gt;. We have used &lt;strong&gt;sleep()&lt;/strong&gt; method to delay execution and call &lt;strong&gt;set_wallpaper()&lt;/strong&gt; every &lt;strong&gt;n&lt;/strong&gt; number of minutes as per user input.&lt;/p&gt;

&lt;p&gt;Ok. All done  now lets run the app 🙂&lt;/p&gt;

&lt;h4&gt;
  
  
  How to run the app?
&lt;/h4&gt;

&lt;p&gt;Run in the CMD as shown below.&lt;br&gt;
To run every minute&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\jaqsp&amp;gt;wallpaper.py -t 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or if you want to change wallpaper every 5 minutes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\jaqsp&amp;gt;wallpaper.py -t 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Hope this post will help you understand some important concepts like how we made http requests and downloaded an image to our local system. Also how we used Windows's system function or(linux's gnome setting) and called it from Python program to change the wallpaper. There are many other parameters available for this system function that we can use to perform some other interesting cases. Visit the link below to know more about this function.&lt;/p&gt;

&lt;p&gt;Visit  &lt;a href="https://github.com/jaqsparow/change-desktop-wallpaper" rel="noopener noreferrer"&gt;my GitHub repository&lt;/a&gt;  to see complete codes. Thanks for reading:)&lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow&lt;/a&gt;  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://requests.readthedocs.io/en/master/" rel="noopener noreferrer"&gt;https://requests.readthedocs.io/en/master/&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>linux</category>
      <category>ubuntu</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How to setup alarm for CPU usage using IFTTT ?</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Fri, 06 Nov 2020 21:33:57 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-setup-alarm-for-cpu-usage-using-ifttt-nah</link>
      <guid>https://dev.to/shaikh/how-to-setup-alarm-for-cpu-usage-using-ifttt-nah</guid>
      <description>&lt;p&gt;Monitoring CPU and memory usage are one of the top todo checklist for a backend engineer. Sometimes you wont even notice when your server is down due to high CPU usage unless you login and manually check the system. In this tutorial, we are going to setup alarm for CPU Usage by most easy way possible(at least I think so) using a simple shell script and IFTTT web hooks. &lt;/p&gt;

&lt;h4&gt;
  
  
  What we are going to learn?
&lt;/h4&gt;

&lt;p&gt;📗 What is IFTTT and how to create an applet?&lt;br&gt;
📙 How to check CPU usage of a system?&lt;br&gt;
📗 How to make web request to IFTTT from your server?&lt;br&gt;
📙 How to write a script to check CPU usage and schedule it on crontab?&lt;/p&gt;
&lt;h3&gt;
  
  
  What is IFTTT?
&lt;/h3&gt;

&lt;p&gt;IFTTT stands for If This, Then That. It is simple web based service that lets you connect your device, server or apps. It is used to create conditional statements or actions that triggered by the changes in your apps, devices or servers. So it helps automate your tasks for your web apps and devices. Lets get started.&lt;/p&gt;

&lt;p&gt;First we need to create a free account and login to  &lt;a href="https://ifttt.com/" rel="noopener noreferrer"&gt;IFTTT&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use IFTTT, we need to create an applet (Free account can create up to 3 applets)&lt;/p&gt;
&lt;h4&gt;
  
  
  How to create applet to set alarm?
&lt;/h4&gt;

&lt;p&gt;Go to the top right and click &lt;strong&gt;Create&lt;/strong&gt; link. You should see similar to below page. Now click Add button on "If this add" link&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604502596167%2FcOG-7hpQR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604502596167%2FcOG-7hpQR.png" alt="iftt1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On next page, You need to select a service. In this case we are going to use IFTTT web hook. So search for web hook and select it to set it up. Click "Receive a web request" and set the event name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604503404609%2FCScCSZIoE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604503404609%2FCScCSZIoE.png" alt="iftt2.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown below we are setting the event name as "high_cpu_usage" and then we will click "Create Trigger" to create the trigger.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604503636298%2FguAT2DByX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604503636298%2FguAT2DByX.png" alt="iftt3.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far we have created the trigger event, next we are going to create action service like what action needs to be taken if the above event is triggered. I am going to set EMAIL service so that an email will be generated when CPU usage will be high. &lt;/p&gt;

&lt;p&gt;So click "Then That Add" and choose EMAIL service and set the subject and body of the email that will be sent to your account. Then Review and finish the setup.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604504102228%2FRfsq-nyWh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604504102228%2FRfsq-nyWh.png" alt="iftt4.PNG"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  How to make web request to IFTTT?
&lt;/h4&gt;

&lt;p&gt;At this moment we are done with setting up our IFTTT applet. Now we need to know the complete web URL of our applet so that we will make web request to notify cpu usage is high. &lt;br&gt;
To do that go to your account and lick my service, then webhooks and then 'documentation' page on the top right corner. You should see something like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604504527269%2Fy0E8O4G4y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604504527269%2Fy0E8O4G4y.png" alt="iftt5.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need to make a small change to add event name to the above URL.&lt;br&gt;
So our correct URL to make web request should look like this and this is how are going to make a web request to IFTTT.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X POST https://maker.ifttt.com/trigger/high_cpu_usage/with/key/boJOWPwAx6x2qjAWr3CUym
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I have deleted &lt;strong&gt;webhook&lt;/strong&gt; created for this tutorial. So please don't use the above key, Its not going to work 😃.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  How to check CPU Usage?
&lt;/h3&gt;

&lt;p&gt;There are many ways to check CPU usage of a server. I have already posted a complete article for CPU Utilization and we are not going to cover in detail here. Please go and check my article if you are interested  &lt;a href="https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux" rel="noopener noreferrer"&gt;https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;For this tutorial, we are going to use VMSTAT command. This command will tell current CPU and memory usage of the system. &lt;br&gt;
Below command will tell CPU usage every second for 5 times. So with this command we can check CPU usage for last 5 seconds. We are going to use this entry in our next shell script to check the cpu usage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VMSTAT 1 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Shell Script to monitor CPU Usage
&lt;/h4&gt;

&lt;p&gt;Since we got the idea how to check CPU Usage lets start writing a simple script to monitor CPU usage.&lt;br&gt;
Here is how our script looks like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/bash
#Set Threshold for CPU Usage
THRESHOLD=90.0
#CPU usage for last 5 seconds
cpu_usage=$(vmstat 1 5 | awk '{if(NR &amp;gt; 2) {sum+=$15}} END {print 100-sum/5}')
if [ $(echo "$cpu_usage &amp;gt; $THRESHOLD" | bc) -eq 1 ]
then
#SET ALARM using IFTTT webhook
curl -X POST https://maker.ifttt.com/trigger/high_cpu_usage/with/key/dhFYYNRL4to_1k5O1NOUd3
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets understand the above script.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First line is the bash interpreter location.&lt;/li&gt;
&lt;li&gt;In the second line we are defining a constant for CPU threshold value.&lt;/li&gt;
&lt;li&gt;In the next line we are executing VMSTAT command and saving CPU usage in a variable named cpu_usage&lt;/li&gt;
&lt;li&gt;In the next line no 4, we are comparing CPU usage value with the threshold value set in the second line. &lt;/li&gt;
&lt;li&gt;If the CPU usage is higher than the threshold, we are making a web request to IFTTT with the use of &lt;strong&gt;CURL&lt;/strong&gt; command, That's it! Lets save this script as &lt;em&gt;cpu_alert.sh&lt;/em&gt; in your system.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Run this CPU usage script every 5 minutes.
&lt;/h4&gt;

&lt;p&gt;With the help of crontab we are going to schedule this shell script ,so that script will be running every 5 minutes to monitor our CPU health.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*/5  *  *  *  *  sh cpu_alert.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setting in crontab, our script will run every 5 minutes and check CPU usage. If you want to know more about how to set crontab and schedule jobs, then check my post  &lt;a href="https://shaikhu.com/how-to-schedule-and-manage-tasks-using-crontab" rel="noopener noreferrer"&gt;How to use crontab&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;If the cpu usage is higher than Threshold, it will make a web request to IFTTT and IFTTT will send us an email alert  😃. Now how easy that was!!!&lt;/p&gt;

&lt;h4&gt;
  
  
  Output of above work
&lt;/h4&gt;

&lt;p&gt;This is how IFTTT email looks like when CPU usage reaches the threshold limit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604510681337%2FTyw6oxi6K.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604510681337%2FTyw6oxi6K.png" alt="gifgit.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;IFTTT is a very easy and also powerful web based service that can be used to connect your apps and devices. As shown in this tutorial, with just few lines of script, we have build a powerful tool to monitor our system's CPU usage. Same logic can be used and extended for memory usage as well.&lt;br&gt;
The amazing thing is that we are not writing codes to send an email. We are just making a web request conditionally to IFTTT and IFTTT is sending us the email alert.&lt;/p&gt;

&lt;p&gt;Hope this tutorial will help you build some amazing apps using IFTTT. Let me know your thoughts in the comment section and dont forget to check my other &lt;strong&gt;How to&lt;/strong&gt; tutorials listed below. Thanks!&lt;/p&gt;

</description>
      <category>linux</category>
      <category>python</category>
      <category>ubuntu</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Flask or bottle</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Thu, 05 Nov 2020 17:54:50 +0000</pubDate>
      <link>https://dev.to/shaikh/flask-or-bottle-5p5</link>
      <guid>https://dev.to/shaikh/flask-or-bottle-5p5</guid>
      <description>&lt;p&gt;Both Flask and Bottle are lightweight micro web framework for Python. I never used bottle but I am going to give it a try? &lt;/p&gt;

&lt;h4&gt;
  
  
  Which one you prefer and why?
&lt;/h4&gt;

</description>
      <category>python</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to trigger a job or program from anywhere with a SMS</title>
      <dc:creator>Javed Shaikh</dc:creator>
      <pubDate>Thu, 05 Nov 2020 16:49:52 +0000</pubDate>
      <link>https://dev.to/shaikh/how-to-trigger-a-job-or-program-from-anywhere-with-a-sms-4pa1</link>
      <guid>https://dev.to/shaikh/how-to-trigger-a-job-or-program-from-anywhere-with-a-sms-4pa1</guid>
      <description>&lt;p&gt;Its holiday season every where and we don't want to open laptop and system all the time when travelling and enjoying our personal time. In this post, I am going to show how we can run some important jobs or programs with just one SMS when we are on the go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;In this tutorial we are going to learn how we can trigger a job or program with just one SMS. For this tutorial we are going to use &lt;strong&gt;Twilio&lt;/strong&gt; API for sending and receiving SMS. With just few lines of codes, we will implement a very basic app to receive sms and trigger jobs based on the sms. So lets check out what we are going to learn in this tutorial.&lt;/p&gt;

&lt;h4&gt;
  
  
  What we will learn in this article?
&lt;/h4&gt;

&lt;p&gt;📗 How to setup programmable SMS api on Twilio? &lt;br&gt;
📙 How to write  a basic Flask app? &lt;br&gt;
📗 How to setup &lt;strong&gt;ngrok&lt;/strong&gt; to access your app outside localhost?&lt;br&gt;
📙 How to run a shell command from Python?&lt;/p&gt;

&lt;p&gt;### How to setup programmable SMS api on Twilio? &lt;/p&gt;

&lt;p&gt;In the very first step, we are going to need a mobile number and API where we can send a SMS and run a script. For this purpose, we are going to use &lt;strong&gt;Twilio&lt;/strong&gt;'s beautiful and easy to use API to setup our SMS app. &lt;/p&gt;
&lt;h4&gt;
  
  
  Get your free $10 credit to use Twilio SMS api
&lt;/h4&gt;

&lt;p&gt;Twilio is providing &lt;strong&gt;$10 credit&lt;/strong&gt; for new user signup and interesting thing about &lt;strong&gt;Twilio trial account&lt;/strong&gt; is that you don't need to have a credit card to try its free service. Please  &lt;a href="//www.twilio.com/referral/ZZMzrs"&gt;click here and visit Twilio&lt;/a&gt;  to create a free account and get $10 credit to use its SMS api during trial period.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once your free account is created, login to your account and create a project from top right corner of your dashboard.&lt;/li&gt;
&lt;li&gt;Then go to &lt;strong&gt;All products &amp;amp; services&lt;/strong&gt;  from the left tab and click ** # Phone numbers*&lt;em&gt;. You should see screen similar to below. Click **Buy a Number&lt;/em&gt;* and get a new number where we will send a SMS.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604168967939%2FH-mXW1yDF.png" alt="twilio.png"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have a number registered, lets go to next step and setup a Flask app.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to write  a basic Flask app?
&lt;/h3&gt;

&lt;p&gt;As I mentioned we are going to use a very basic Flask app. Before writing our Flask app, lets install virtual environment so that our app will be running independently with the packages installed within the environment.&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 1: Create virtual environment
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Lets install virtual environment.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install virtualenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Now create a virtual environment within a new directory smsapp
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shaikh@ubuntu:~/scripts/smsapp$ virtualenv venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Activate the virtual environment
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shaikh@ubuntu:~/scripts/smsapp$ source venv/bin/activate
(venv) shaikh@ubuntu:~/scripts/smsapp$ 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Step 2: Install Flask
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Install Flask app using below entry
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install flask
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Lets write very basic flask app with below lines of code.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello_world&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Run this app with &lt;em&gt;python3 app.py&lt;/em&gt; as shown below and check the result at &lt;em&gt;&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604172015050%2F2oehMXhN7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604172015050%2F2oehMXhN7.png" alt="flask1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should see the output "Hello World!" in your localhost at port 5000. As shown above you can check the output at &lt;em&gt;&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 3: How to install and access app using ngrok
&lt;/h4&gt;

&lt;p&gt;Now that we have installed our flask app and able to run it in our localhost but we cannot access our app outside our local network. To access this app over internet lets install &lt;strong&gt;ngrok&lt;/strong&gt; which will allow us to expose our app over the internet.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install this with below command (for Ubuntu)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;snap install ngrok
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Activate ngrok to expose our app to the internet.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 app.py &amp;amp;
ngrok http 5000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is how it will look once we run above commands&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604172783909%2F_xoC7Pzak.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604172783909%2F_xoC7Pzak.png" alt="ngrok1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We need to use the ngrok http URL from above output. Lets checkout the app at this url on any device over the internet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604172852487%2Fx04qK7TFX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604172852487%2Fx04qK7TFX.png" alt="ngrok2.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  How to run a shell command from Python?
&lt;/h3&gt;

&lt;p&gt;To run a shell command from Python, we will use &lt;strong&gt;OS&lt;/strong&gt; module's special function &lt;strong&gt;popen()&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;os.popen(command)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this is how our Flask app looks like if we want to run a command using &lt;strong&gt;popen()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello_world&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/command&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;POST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/usr/bin/python3 ~/scripts/send_alert.py&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Command executed&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that we have created a new route &lt;strong&gt;/command&lt;/strong&gt; to execute the command to run another python script &lt;strong&gt;send_alert.py&lt;/strong&gt;. You can execute any command you want. For this example I am running a script to send email alert.&lt;/p&gt;

&lt;p&gt;So this is how our Flask app will trigger a new process and execute the command. Now we are going to configure &lt;strong&gt;Twilio&lt;/strong&gt; so that when we send sms to its number, Twilio should trigger a HTTP request to our &lt;strong&gt;/Command&lt;/strong&gt; route.&lt;/p&gt;

&lt;p&gt;Lets head over to &lt;strong&gt;Twilio&lt;/strong&gt; again and click the mobile number you purchased in previous steps and then update the webhook  for "A message comes in" as shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604249417727%2Fh7eaz9GqH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604249417727%2Fh7eaz9GqH.png" alt="twiliosms.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now when we send a sms to Twilio number, It will make HTTP Post request to above link and when it does, our &lt;em&gt;command()&lt;/em&gt; function is going to be executed 😃&lt;/p&gt;

&lt;p&gt;Now if you want to run multiple jobs based on SMS body (like JOB1,JOB2) we need to install Twilio library so that we can parse SMS and run the jobs accordingly.&lt;br&gt;
Here is the app.py looks like with that feature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;twilio.twiml.messaging_response&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;MessagingResponse&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/command&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;POST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;From&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;message_body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Body&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message_body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;command &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; executed&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;job1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/usr/bin/python3 ~/scripts/job1.py&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;job2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/usr/bin/python3 ~/scripts/job2.py&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid request

    resp = MessagingResponse()
    resp.message(response)
    return str(resp)

if __name__ == &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;:
    app.run()

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As shown above, with the help of &lt;strong&gt;Twilio&lt;/strong&gt; library we are able to parse text message and run jobs based on incoming message content. Now if we send a text 'JOB1' to the &lt;strong&gt;Twilio&lt;/strong&gt; mobile number, Twilio will make a HTTP POST request to our flask app and execute our job1 script. Once job1 script is executed a return sms will be received on your mobile number that 'command executed' else you will receive 'Invalid request' in case you send some invalid job requests:)&lt;br&gt;
In above Flask app, you can replace the jobs or program names and add any number of programs or commands or even shell scripts if you want to execute them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Output:
&lt;/h4&gt;

&lt;p&gt;Here is the output of above script and this is how our app should work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604266100085%2FFuxnbdSlP.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1604266100085%2FFuxnbdSlP.jpeg" alt="1IMG_9085.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Congratulations on building your SMS app to run a job with just a SMS🥳.. In this article we learned some interesting concepts like how to run a shell command from Python script and how to integrate third party sms api to our FLASK app. &lt;/p&gt;

&lt;p&gt;Hope this will help you build some amazing sms apps. Let me know your thoughts in the comment section and dont forget to read my other interesting posts in below links.&lt;/p&gt;

&lt;p&gt;Thanks for reading this article. Hit like if you want more such posts 😊&lt;/p&gt;

&lt;p&gt;Visit my GitHub page for complete code at  &lt;a href="https://github.com/jaqsparow/run-a-job-by-sms" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt; &lt;a href="https://docs.python.org/3/library/os.html#os.popen" rel="noopener noreferrer"&gt;https://docs.python.org/3/library/os.html#os.popen&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://www.twilio.com/docs/sms" rel="noopener noreferrer"&gt;Twilio sms api&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://flask.palletsprojects.com/en/1.1.x/" rel="noopener noreferrer"&gt;Flask documentaion&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;  &lt;a href="https://ngrok.com/docs" rel="noopener noreferrer"&gt;ngrok&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note: This article was originally published at &lt;a href="https://shaikhu.com/how-to-trigger-a-job-or-program-from-anywhere-with-a-sms" rel="noopener noreferrer"&gt;https://shaikhu.com/how-to-trigger-a-job-or-program-from-anywhere-with-a-sms&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>flask</category>
      <category>twilio</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
