( exec sh -i 3<<SCRIPT 4<&0 <&3 ⏎
echo "do this thing"
echo "do that thing"
exec 3>&- <&4
SCRIPT
)
สิ่งนี้ทำได้ดีกว่าจากสคริปต์ด้วยexec $0.
หรือหากหนึ่งในตัวอธิบายไฟล์เหล่านั้นนำไปยังอุปกรณ์ปลายทางที่ไม่ได้ใช้งานในปัจจุบันจะช่วยได้ - คุณต้องจำไว้กระบวนการอื่น ๆ ก็ต้องการตรวจสอบเทอร์มินัลเช่นกัน
และถ้าหากเป้าหมายของคุณคือเพื่อรักษาสภาพแวดล้อมของสคริปต์หลังจากเรียกใช้งานคุณอาจจะได้รับบริการที่ดีขึ้นด้วย:
. ./script
เชลล์.dot
และbash's source
ไม่ใช่หนึ่งเดียว - เชลล์.dot
คือ POSIX ที่ระบุว่าเป็นเชลล์ในตัวพิเศษและใกล้เคียงกับการรับประกันมากที่สุดเท่าที่คุณจะได้รับแม้ว่าจะไม่ได้รับประกันว่าจะมี ...
แม้ว่าข้างต้นควรทำตามที่คุณคาดหวังกับปัญหาเล็กน้อย ตัวอย่างเช่นคุณสามารถ:
( exec sh -i 3<<SCRIPT 4<&0 <&3 ⏎
echo "do this thing"
echo "do that thing"
$(cat /path/to/script)
exec 3>&- <&4
SCRIPT
)
เชลล์จะรันสคริปต์ของคุณและนำคุณกลับไปที่พรอมต์แบบโต้ตอบตราบใดที่คุณหลีกเลี่ยงexit
การเชลล์จากสคริปต์ของคุณนั่นคือหรือทำให้พื้นหลังกระบวนการของคุณ - ซึ่งจะเชื่อมโยง i / o ของคุณกับ/dev/null.
การสาธิต:
% printf 'echo "%s"\n' "These lines will print out as echo" \
"statements run from my interactive shell." \
"This will occur before I'm given the prompt." >|/tmp/script
% ( exec sh -i 3<<SCRIPT 4<&0 <&3
echo "do this thing"
echo "do that thing"
$(cat /tmp/script)
exec 3>&- <&4
SCRIPT
)
sh-4.3$ echo "do this thing"
do this thing
sh-4.3$ echo "do that thing"
do that thing
sh-4.3$ echo "These lines will print out as echo"
These lines will print out as echo
sh-4.3$ echo "statements run from my interactive shell."
statements run from my interactive shell.
sh-4.3$ echo "This will occur before I'm given the prompt."
This will occur before I'm given the prompt.
sh-4.3$ exec 3>&- <&4
sh-4.3$
หลาย JOBS
ฉันคิดว่าคุณควรทำความคุ้นเคยกับตัวเลือกการจัดการงานในตัวของเชลล์ @Kiwy และ @jillagre ต่างก็ได้สัมผัสกับสิ่งนี้ในคำตอบของพวกเขาแล้ว แต่มันอาจรับประกันรายละเอียดเพิ่มเติม และผมได้กล่าวแล้วหนึ่ง POSIX ระบุเปลือกพิเศษในตัว แต่set, jobs, fg,
และbg
มีอีกไม่กี่และเป็นคำตอบอื่นแสดงให้เห็นtrap
และkill
มีอีกสองคนยังคง
หากคุณยังไม่ได้รับการแจ้งเตือนทันทีเกี่ยวกับสถานะของกระบวนการที่ทำงานเบื้องหลังพร้อมกันนั่นเป็นเพราะตัวเลือกเชลล์ปัจจุบันของคุณถูกตั้งค่าเป็นค่าเริ่มต้นที่ระบุโดย POSIX ของ-m
แต่คุณสามารถรับสิ่งเหล่านี้แบบอะซิงโครนัสset -b
แทนได้
% man set
−b This option shall be supported if the implementation supports the
User Portability Utilities option. It shall cause the shell to
notify the user asynchronously of background job completions. The
following message is written to standard error:
"[%d]%c %s%s\n", <job-number>, <current>, <status>, <job-name>
where the fields shall be as follows:
<current> The character '+' identifies the job that would be
used as a default for the fg or bg utilities; this
job can also be specified using the job_id "%+" or
"%%". The character '−' identifies the job that
would become the default if the current default job
were to exit; this job can also be specified using
the job_id "%−". For other jobs, this field is a
<space>. At most one job can be identified with '+'
and at most one job can be identified with '−'. If
there is any suspended job, then the current job
shall be a suspended job. If there are at least two
suspended jobs, then the previous job also shall be a
−m This option shall be supported if the implementation supports the
User Portability Utilities option. All jobs shall be run in their
own process groups. Immediately before the shell issues a prompt
after completion of the background job, a message reporting the
exit status of the background job shall be written to standard
error. If a foreground job stops, the shell shall write a message
to standard error to that effect, formatted as described by the
jobs utility. In addition, if a job changes status other than
exiting (for example, if it stops for input or output or is
stopped by a SIGSTOP signal), the shell shall write a similar
message immediately prior to writing the next prompt. This option
is enabled by default for interactive shells.
คุณลักษณะพื้นฐานมากของระบบ Unix-based signals
เป็นวิธีการของพวกเขาในการจัดการกระบวนการ ฉันเคยอ่านบทความ enlighteningในเรื่องที่ likens กระบวนการนี้เพื่ออธิบาย Douglas Douglas Adams 'ของดาวเคราะห์NowWhat:
"ในคู่มือ Hitchhiker สู่กาแล็กซี่, ดักลาสอดัมส์กล่าวถึงดาวเคราะห์ที่น่าเบื่ออย่างยิ่งซึ่งอาศัยอยู่โดยกลุ่มมนุษย์ที่มีความกดดันและสัตว์บางชนิดที่มีฟันแหลมคมซึ่งสื่อสารกับมนุษย์ด้วยการกัดพวกมันอย่างหนักที่ต้นขา คล้ายกับ UNIX ซึ่งเคอร์เนลสื่อสารกับกระบวนการโดยส่งสัญญาณอัมพาตหรืออันตรายถึงขั้นตอนกระบวนการอาจดักจับสัญญาณบางอย่างและพยายามปรับให้เข้ากับสถานการณ์
kill signals
นี้จะหมายถึง
% kill -l
> HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS
อย่างน้อยสำหรับฉันข้อความข้างต้นตอบคำถามมากมาย ยกตัวอย่างเช่นผมมักจะคิดว่ามันแปลกมากและไม่ได้ทั้งหมดที่ใช้งานง่ายว่าถ้าผมต้องการที่จะตรวจสอบdd
ขั้นตอนที่ผมต้องkill
มัน หลังจากอ่านแล้วมันก็สมเหตุสมผล
ฉันจะบอกว่าส่วนใหญ่ของพวกเขาไม่ได้พยายามที่จะปรับตัวให้เข้ากับเหตุผลที่ดี - มันอาจเป็นเรื่องที่น่ารำคาญยิ่งกว่ามันจะเป็นประโยชน์ที่จะมีกระบวนการมากมายที่ส่งสแปมเทอร์มินัลของคุณ .
ทั้งนี้ขึ้นอยู่กับการกำหนดค่า terminal ของคุณ(ซึ่งคุณสามารถตรวจสอบกับstty -a
) , CTRL+Z
มีแนวโน้มที่จะกำหนดให้ส่งต่อSIGTSTP
ให้กับผู้นำกลุ่มกระบวนการเบื้องหน้าในปัจจุบันซึ่งมีแนวโน้มที่เปลือกของคุณและที่ยังควรจะกำหนดค่าเริ่มต้นให้trap
สัญญาณที่และระงับคำสั่งสุดท้ายของคุณ อีกครั้งในขณะที่คำตอบของ @jillagre และ @Kiwy ปรากฏขึ้นพร้อมกันคุณจะไม่หยุดคุณจากการปรับฟังก์ชั่นนี้ให้เหมาะกับวัตถุประสงค์ของคุณตามที่คุณต้องการ
SCREEN JOBS
ดังนั้นเพื่อใช้ประโยชน์จากคุณสมบัติเหล่านี้คาดว่าคุณจะเข้าใจและปรับแต่งการจัดการตามความต้องการของคุณก่อน ตัวอย่างเช่นฉันเพิ่งพบscreenrc นี้ใน Githubที่มีscreen
การเชื่อมโยงคีย์สำหรับSIGTSTP
:
# hitting 'C-z C-z' will run Ctrl+Z (SIGTSTP, suspend as usual)
bind ^Z stuff ^Z
# hitting 'C-z z' will suspend the screen client
bind z suspend
ซึ่งจะทำให้เป็นเรื่องง่ายที่จะระงับกระบวนการที่เรียกใช้เป็นscreen
กระบวนการลูกหรือscreen
กระบวนการลูกเองตามที่คุณต้องการ
และทันทีหลังจากนั้น:
% fg
หรือ:
% bg
จะเบื้องหน้าหรือเบื้องหลังกระบวนการตามที่คุณต้องการ jobs
ในตัวสามารถให้คุณรายการเหล่านี้ได้ตลอดเวลา การเพิ่ม-l
ตัวถูกดำเนินการจะรวมถึงรายละเอียด pid