자바스크립트를 활성화 해주세요

p021 Crontab에서 Pwsh 스크립트 사용하기

 ·  ☕ 2 min read

윈도우즈용 pwsh에서는 되지만 리눅스용 pwsh에서는 다음의 두 가지 기능이 안됩니다.

  • WMI or CIM
  • Scheduled PowerShell Jobs

이 이외에도 alias, Remoting via SSH, Help 등이 윈도우즈에서의 동작과 다릅니다.

그런데 정기적으로 pwsh script를 실행하고 싶습니다. 이렇게 할 수 있을까요?

crontab

역시 crontab을 사용할 수 밖에 없습니다.

그럼 어떻게 작성할까요, 아, SHELL 변수가 있다고 하니 다음과 같이 작성하면 어떨까요

SHELL=/usr/bin/pwsh
*/1 * * * * /Do-Something.ps1
*/1 * * * * /Do-Else.ps1
*/1 * * * * /Do-Nothing.ps1
*/1 * * * * /Do-TheBestThing.ps1
SHELL=/usr/bin/bash
*/1 * * * * /test.sh
*/1 * * * * /wow.sh

안타깝지만 SHELL 은 global 변수입니다. crontab의 설명서를 보면 Switch해서 사용할 수 없습니다.

/etc/ 아래에는 다음과 같은 여러 디렉토리가 있습니다.

cron.d        cron.daily    cron.hourly   cron.monthly  cron.weekly

pwsh 스크립트의 첫줄에 다음과 같이 shebang을 작성합니다.

#! /usr/bin/pwsh 

그리고 crontab에는 다음과 같이 작성합니다.

*/1 * * * * /home/xxxxxxx/pwsh/Invoke-FileZipper.ps1 -FileToCompress myfile1

goormide에서 default shell 을 pwsh로 변경하기 (실패)

더불어 goormide에 로그인 했을 때 default shell을 변경해 보았습니다.

1
2
3
4
5
6
7
8
9
PS /root>
PS /root> which pwsh
/opt/microsoft/powershell/7/pwsh
PS /root> chsh
Changing the login shell for root
Enter the new value, or press ENTER for the default
        Login Shell [/bin/bash]: /opt/microsoft/powershell/7/pwsh
PS /root>

결과는? 원하는 대로 움직이지 않았습니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 cd /workspace/pwsh; clear
export LANG=ko_KR.UTF-8;export LANGUAGE=ko_KR.UTF-8;locale-gen;source ~/.profile; [ ! -f ~/.custom_env ] || source ~/.custom_env; clear;
cd /workspace/pwsh; clear
^[[4;1R^[[4;8Rroot@goorm:~# 8R
bash: 8R: 명령어를 찾을 수 없음
root@goorm:~#
root@goorm:~#
root@goorm:~# ;15R
bash: syntax error near unexpected token `;'
root@goorm:~#

다른 명령으로도 시도해 봤습니다.

1
chsh -s /usr/bin/pwsh root

역시 실패. 이건 pwsh의 문제가 아니라 혹시 goormide의 사양인 것 같습니다. 뭔가 다른 방법을 찾아야 할 것 같습니다.

레퍼런스

공유하기

tkim
글쓴이
tkim
Software Engineer