Activate virtual environment in Crontab task

Introduction

工作中遇到一个需求, 需要在后台起一个监督进程(假设进程名为data_process)的脚本, 这个脚本需要在虚拟环境 venv_py36 中执行, 因此写了一个脚本, 每分钟执行一次, 检测进程数量, 如果小于1, 则唤起虚拟环境并在虚拟环境中重启进程. 但是完成需求的过程中发现了不少有意思的点.

Difference between sh and bash

根据Stack Overflow上的问题 Difference between sh and bash 了解到, sh其实和bash并不完全一样.

What is sh

sh (or the Shell Command Language) is a programming language described by thePOSIX standard. It has many implementations (ksh88, dash, …). bash can also be considered an implementation of sh (see below).

sh(也被称为Shell Command Language) 是一种基于POSIX标准的编程语言. 它有很多的实现(ksh88, dash,…). bash也被认为是sh的一种实现.

Because sh is a specification, not an implementation, /bin/sh is a symlink (or a hard link) to an actual implementation on most POSIX systems.
因为sh是一种规范而非实现, 所以在大部分的POSIX系统中, /bin/sh都是通过系统连接(或者是硬链接)连接到某一种具体实现.

What is bash

bash started as an sh-compatible implementation (although it predates the POSIX standard by a few years), but as time passed it has acquired many extensions. Many of these extensions may change the behavior of valid POSIX shell scripts, so by itself bash is not a valid POSIX shell. Rather, it is a dialect of the POSIX shell language.

bash 开始作为一种适配sh的实现(尽管它反而早于POSIX标准几年问世), 但是随着时间的推移, bash获得了更多的扩展. 这些扩展中很多都改变了标准POSIX脚本的行为, 所以bash本身并不是一个标准的符合POSIX标准的shell. 更形象一点来说, bashPOSIX脚本语言的一种方言.

bash supports a --posix switch, which makes it more POSIX-compliant. It also tries to mimic POSIX if invoked as sh.

bash 支持 --posix 切换, 能够更适配POSIX.

References

使用shell脚本激活VirtualEnv似乎不工作
如何在服务器(Linux)里面定时(crontab)执行需要虚拟环境(venv)的代码(爬虫)
Difference between sh and bash
Linux下使用crontab定时激活conda虚拟python环境
Linux下使用crontab定时激活conda虚拟python环境

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据