在bash / python / perl脚本中运行一系列交互式shell命令

时间:2022-02-07 08:22:40

Currently, I do the following steps:

目前,我执行以下步骤:

a. Grep for pid of a process and kill it.

一个。 Grep用于进程的pid并将其杀死。

ps -aux | grep foo.bar  # process of interest
kill -9 pid_of_foo.bar  # kill the process

b. start virtualenv

湾启动virtualenv

cd {required_folder}
sudo virtualenv folder/
cd {folder2}
source bin/activate

c. Start the manage.py in shell mode

C。以shell模式启动manage.py.

cd {required folder}
sudo python manage.py shell

d. In the interactive manage shell, execute the following commands:

d。在交互式管理shell中,执行以下命令:

from core import *
foo.bar.bz.clear.state()
exit

e. Execute a script

即执行脚本

   /baz/maz/foo

In bash we can write down a series of commands, however Is it possible to run the interactive shell in django using bash and execute commands? I was wondering if above steps can be scriptified.

在bash中我们可以写下一系列命令,但是是否可以使用bash和execute命令在django中运行交互式shell?我想知道上述步骤是否可以编写脚本。

Thanks

谢谢

1 个解决方案

#1


1  

You need a script like this one:

你需要一个像这样的脚本:

#!/bin/bash

# kill all foo.bar's instances
for pid in $(ps -aux | grep foo.bar | grep -v grep | awk '{print $2;}'); do
    kill $pid
done

# start virtualenv
cd {required_folder}
...

# Start the manage.py in shell mode
cd {required folder}
cat << EOF | sudo python manage.py shell
from core import *
foo.bar.bz.clear.state()
exit
EOF

# Execute a script 
/baz/maz/foo

The key point of the script is HEREDOC python snippet. Take a look at the example I've just tried in a console:

脚本的关键点是HEREDOC python片段。看看我刚刚在控制台中尝试过的示例:

[alex@galene ~]$ cat <<EOF_MARK | python -
> import sys
> print "Hello, world from python %s" % sys.version
> exit
> EOF_MARK
Hello, world from python 2.7.6 (default, Nov 22 2013, 22:57:56)
[GCC 4.7.2 20121109 (ALT Linux 4.7.2-alt7)]
[alex@galene ~]$ _

#1


1  

You need a script like this one:

你需要一个像这样的脚本:

#!/bin/bash

# kill all foo.bar's instances
for pid in $(ps -aux | grep foo.bar | grep -v grep | awk '{print $2;}'); do
    kill $pid
done

# start virtualenv
cd {required_folder}
...

# Start the manage.py in shell mode
cd {required folder}
cat << EOF | sudo python manage.py shell
from core import *
foo.bar.bz.clear.state()
exit
EOF

# Execute a script 
/baz/maz/foo

The key point of the script is HEREDOC python snippet. Take a look at the example I've just tried in a console:

脚本的关键点是HEREDOC python片段。看看我刚刚在控制台中尝试过的示例:

[alex@galene ~]$ cat <<EOF_MARK | python -
> import sys
> print "Hello, world from python %s" % sys.version
> exit
> EOF_MARK
Hello, world from python 2.7.6 (default, Nov 22 2013, 22:57:56)
[GCC 4.7.2 20121109 (ALT Linux 4.7.2-alt7)]
[alex@galene ~]$ _