Skip to content

Commit

Permalink
Site updated: 2024-02-21 23:11:37
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenchi Pan committed Feb 21, 2024
1 parent e7292e1 commit 765d098
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 8 additions & 4 deletions 2024/02/20/linux-common-command/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<meta property="og:description" content="See the log from systemd service: journalctl Common usage journalctl -u UnitName --since today Use right arrow key to scroll right. Or press -S and Enter in less to toggle word wrap. Also see DigitalO">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-02-20T08:53:21.000Z">
<meta property="article:modified_time" content="2024-02-21T15:09:18.235Z">
<meta property="article:modified_time" content="2024-02-21T15:11:25.715Z">
<meta property="article:author" content="Black Pan">
<meta name="twitter:card" content="summary">

Expand Down Expand Up @@ -189,7 +189,7 @@ <h1 class="post-title" itemprop="name headline">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2024-02-21 23:09:18" itemprop="dateModified" datetime="2024-02-21T23:09:18+08:00">2024-02-21</time>
<time title="修改时间:2024-02-21 23:11:25" itemprop="dateModified" datetime="2024-02-21T23:11:25+08:00">2024-02-21</time>
</span>


Expand Down Expand Up @@ -217,15 +217,19 @@ <h2 id="show-colorful-output">Show colorful output:</h2>
<h2 id="continuously-print-new-logs">Continuously print new logs</h2>
<p><code>journalctl --follow --no-tail</code> is used when you want all previous lines (better than -n 100000 or arbitrary big line count)</p>
<h2 id="restrict-the-output-to-latest-run">Restrict the output to latest run:</h2>
<p><a target="_blank" rel="noopener" href="https://serverfault.com/questions/768901/is-there-a-way-to-make-journalctl-show-logs-from-the-last-time-foo-service-ran">TL;DR</a>: Use the one liner: - System level: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value service_name.service`</span><br></pre></td></tr></table></figure> - User level: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value service_name.service --user`</span><br></pre></td></tr></table></figure></p>
<p><a target="_blank" rel="noopener" href="https://serverfault.com/questions/768901/is-there-a-way-to-make-journalctl-show-logs-from-the-last-time-foo-service-ran">TL;DR</a>: Use the one liner:</p>
<ul>
<li>System level: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value service_name.service`</span><br></pre></td></tr></table></figure></li>
<li>User level: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value service_name.service --user`</span><br></pre></td></tr></table></figure></li>
</ul>
<h1 id="pythonconda-in-systemd-service">Python(conda) in systemd service</h1>
<p>Create a systemd Service File: Create a .service file (e.g., my_server.service) in a location of your choice (usually <code>/etc/systemd/system/</code> for system-level services or <code>~/.config/systemd/user/</code> for user-level services). Open the file for editing.</p>
<p>Define the Service:</p>
<ol type="1">
<li>Add the following content to your .service file: <figure class="highlight toml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[Unit]</span></span><br><span class="line"><span class="attr">Description</span>=My Python Server</span><br><span class="line"></span><br><span class="line"><span class="section">[Service]</span></span><br><span class="line"><span class="attr">Type</span>=simple</span><br><span class="line"><span class="attr">ExecStart</span>=/path/to/conda/envs/env_name/bin/python python_file.py</span><br><span class="line"><span class="attr">WorkingDirectory</span>=/path/to/working/directory</span><br><span class="line"></span><br><span class="line"><span class="section">[Install]</span></span><br><span class="line"><span class="attr">WantedBy</span>=default.target</span><br></pre></td></tr></table></figure></li>
<li>Replace <code>/path/to/conda/envs/env_name</code> with the actual path to your Conda environment. Adjust the WorkingDirectory to the directory where your startup.py resides.</li>
<li>Enable and Start the Service:</li>
</ol>
<p>Enable and Start the Service:</p>
<ul>
<li><p>If you’re running this as a user-level service, use: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl --user <span class="built_in">enable</span> my_server.service</span><br><span class="line">systemctl --user start my_server.service</span><br></pre></td></tr></table></figure></p></li>
<li><p>For system-level services, use: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl <span class="built_in">enable</span> my_server.service</span><br><span class="line">sudo systemctl start my_server.service</span><br></pre></td></tr></table></figure> ## Activate a conda env in systemd service https://github.com/mmaz/systemd-conda</p></li>
Expand Down
10 changes: 7 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ <h2 class="post-title" itemprop="name headline">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2024-02-21 23:09:18" itemprop="dateModified" datetime="2024-02-21T23:09:18+08:00">2024-02-21</time>
<time title="修改时间:2024-02-21 23:11:25" itemprop="dateModified" datetime="2024-02-21T23:11:25+08:00">2024-02-21</time>
</span>


Expand Down Expand Up @@ -214,15 +214,19 @@ <h2 id="show-colorful-output">Show colorful output:</h2>
<h2 id="continuously-print-new-logs">Continuously print new logs</h2>
<p><code>journalctl --follow --no-tail</code> is used when you want all previous lines (better than -n 100000 or arbitrary big line count)</p>
<h2 id="restrict-the-output-to-latest-run">Restrict the output to latest run:</h2>
<p><a target="_blank" rel="noopener" href="https://serverfault.com/questions/768901/is-there-a-way-to-make-journalctl-show-logs-from-the-last-time-foo-service-ran">TL;DR</a>: Use the one liner: - System level: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value service_name.service`</span><br></pre></td></tr></table></figure> - User level: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value service_name.service --user`</span><br></pre></td></tr></table></figure></p>
<p><a target="_blank" rel="noopener" href="https://serverfault.com/questions/768901/is-there-a-way-to-make-journalctl-show-logs-from-the-last-time-foo-service-ran">TL;DR</a>: Use the one liner:</p>
<ul>
<li>System level: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value service_name.service`</span><br></pre></td></tr></table></figure></li>
<li>User level: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value service_name.service --user`</span><br></pre></td></tr></table></figure></li>
</ul>
<h1 id="pythonconda-in-systemd-service">Python(conda) in systemd service</h1>
<p>Create a systemd Service File: Create a .service file (e.g., my_server.service) in a location of your choice (usually <code>/etc/systemd/system/</code> for system-level services or <code>~/.config/systemd/user/</code> for user-level services). Open the file for editing.</p>
<p>Define the Service:</p>
<ol type="1">
<li>Add the following content to your .service file: <figure class="highlight toml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[Unit]</span></span><br><span class="line"><span class="attr">Description</span>=My Python Server</span><br><span class="line"></span><br><span class="line"><span class="section">[Service]</span></span><br><span class="line"><span class="attr">Type</span>=simple</span><br><span class="line"><span class="attr">ExecStart</span>=/path/to/conda/envs/env_name/bin/python python_file.py</span><br><span class="line"><span class="attr">WorkingDirectory</span>=/path/to/working/directory</span><br><span class="line"></span><br><span class="line"><span class="section">[Install]</span></span><br><span class="line"><span class="attr">WantedBy</span>=default.target</span><br></pre></td></tr></table></figure></li>
<li>Replace <code>/path/to/conda/envs/env_name</code> with the actual path to your Conda environment. Adjust the WorkingDirectory to the directory where your startup.py resides.</li>
<li>Enable and Start the Service:</li>
</ol>
<p>Enable and Start the Service:</p>
<ul>
<li><p>If you’re running this as a user-level service, use: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl --user <span class="built_in">enable</span> my_server.service</span><br><span class="line">systemctl --user start my_server.service</span><br></pre></td></tr></table></figure></p></li>
<li><p>For system-level services, use: <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl <span class="built_in">enable</span> my_server.service</span><br><span class="line">sudo systemctl start my_server.service</span><br></pre></td></tr></table></figure> ## Activate a conda env in systemd service https://github.com/mmaz/systemd-conda</p></li>
Expand Down

0 comments on commit 765d098

Please sign in to comment.