( 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नहीं एक और एक ही कर रहे हैं - खोल के .dotPOSIX एक विशेष खोल अंतर्निहित रूप में निर्दिष्ट किया है और के रूप में पास की गारंटी की जा रही के रूप में आप प्राप्त कर सकते हैं करने के लिए इसलिए है, हालांकि यह किसी भी तरह से एक गारंटी यह वहाँ हो जाएगा ...
यद्यपि उपरोक्त आपको कम मुद्दे के साथ अपेक्षा के अनुसार करना चाहिए। उदाहरण के लिए, आप कर सकते हैं:
( 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.
यूनिक्स-आधारित प्रणालियों की एक बहुत ही मौलिक विशेषता प्रक्रिया को संभालने की उनकी विधि है signals। मैंने एक बार एक ज्ञानवर्धक लेख पढ़ा इस विषय पर डगलस एडम्स के ग्रह नोवेट के विवरण के लिए इस प्रक्रिया की तुलना करता है :
"द हिचहाइकर गाइड टू द गैलेक्सी में, डगलस एडम्स ने एक बेहद सुस्त ग्रह का उल्लेख किया है, जो उदास मनुष्यों के झुंड में बसा हुआ है और जानवरों की एक निश्चित नस्ल है जिसके तेज दांत हैं जो जांघों में बहुत मुश्किल से काटकर मनुष्यों के साथ संवाद करते हैं। यह हड़ताली है। 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। जिसे पढ़ने के बाद समझ में आया।
मैं कहूंगा कि उनमें से अधिकांश अनुकूलन करने की कोशिश नहीं करते हैं अच्छे कारण के - यह एक बड़ी गड़बड़ी हो सकती है, क्योंकि यह एक वरदान होगा प्रक्रियाओं का एक गुच्छा आपके टर्मिनल को स्पैम करने से जो भी जानकारी उनके डेवलपर्स के लिए आपके लिए महत्वपूर्ण हो सकती है। ।
आपके टर्मिनल कॉन्फ़िगरेशन (जो आप के साथ जांच कर सकते हैं stty -a) के आधार पर , CTRL+Zसंभावित रूप से SIGTSTPवर्तमान अग्रभूमि प्रक्रिया समूह के नेता को अग्रेषित करने के लिए सेट किया जाता है, जो आपके शेल की संभावना है, और जिसे डिफ़ॉल्ट रूप से trapउस सिग्नल से भी कॉन्फ़िगर किया जाना चाहिए और आपकी अंतिम कमांड को निलंबित करना चाहिए । फिर से, @jillagre और @Kiwy के जवाबों को एक साथ दिखाने के रूप में, इस कार्यक्षमता को अपने उद्देश्य के लिए पसंद करने पर आपको रोकना नहीं है।
SCREEN JOBS
इसलिए इन सुविधाओं का लाभ उठाने के लिए यह अपेक्षित है कि आप पहले उन्हें समझें और उनकी हैंडलिंग को अपनी आवश्यकताओं के अनुसार अनुकूलित करें। उदाहरण के लिए, मैंने सिर्फ 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ओपेरंड को जोड़ने से पिड विवरण शामिल होगा।