Existe uma maneira de fornecer tarefas seriais no terminal enquanto uma tarefa está sendo executada (sem usar um arquivo)?

14

Suponha que há duas tarefas t1 , t2 , que podem ser executadas de maneira serial, conforme abaixo:

t1 ; t2
# OR
t1 && t2

Suponha agora que esqueci de executar t2 e t1 já está em execução; posso adicionar t2 ao pipeline para que ele seja executado depois que t1 terminar?

    
por Abhimanyu Gupta 08.05.2018 / 07:46

2 respostas

19

Sim, você pode:

  1. Pause o trabalho atualmente em execução com o suspender caractere pressionando Ctrl + Z .
  2. Digite fg ou % , adicione o que você deseja à lista e execute-a, por exemplo:
    fg ; systemctl suspend # or
    % ; systemctl suspend
    Como fg retorna o valor de retorno do job, os operadores de lista como && e || funcionam como esperado:
    fg && echo "Finished successfully!" # or
    % && echo "Finished successfully!"

man bash / JOB CONTROL diz sobre o caractere de suspensão :

Typing the suspend character (typically ^Z, Control-Z) while a process is running causes that process to be stopped and returns control to bash. (…) The user may then manipulate the state of this job, using the bg command to continue it in the background, the fg command to continue it in the foreground, or the kill command to kill it. A ^Z takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.

fg é explicado em man bash / SHELL BUILTIN COMMANDS :

fg [jobspec]
Resume jobspec in the foreground, and make it the current job. If jobspec is not present, the shell's notion of the current job is used. The return value is that of the command placed into the foreground, or failure if run when job control is disabled or, when run with job control enabled, if jobspec does not specify a valid job or jobspec specifies a job that was started without job control.

Leitura adicional (além de man bash ) no controle de trabalho:

por dessert 08.05.2018 / 07:57
2

Eu vi este método aqui: link

onde primeiro você faz Ctrl + z para parar (suspender) o executável, então você executa o comando perdido da seguinte forma: fg && ./missed_cmd.sh e ele será executado assim que como o fg termina.

O fg (comando em primeiro plano) colocará o trabalho suspenso on-line e o && garantirá que o comando perdido seja executado apenas se o primeiro comando for bem-sucedido.

    
por George Udosen 08.05.2018 / 08:01