tjex
tjex

Reputation: 256

Shell script calling ''zsh -l && some_command'' never running some_command

I have this basic shell script that I'm invoking via an alias:

#!/bin/sh

cd /Users/tillman/t-root/dev/apps/actual-server && 
env /usr/bin/arch -x86_64 /bin/zsh --login && 
yarn start

It moves directory, changes the arch but does not execute yarn start

If I break this up into two consecutive commands (executing the first and then the second within iterm via different aliases), it works:

alias = intel

env /usr/bin/arch -x86_64 /bin/zsh --login

alias = abudget

cd /Users/tillman/t-root/dev/apps/actual-server
yarn start

Output:

 ~ intel                                                                                                          ✔
 ~ abudget                                                                                                        ✔
yarn run v1.22.19
$ node app
Initializing Actual with user file dir: /Users/tillman/t-root/dev/apps/actual-server/user-files
Listening on 0.0.0.0:5006...

Why is it that the first option, with all commands in one script, does not work?

Upvotes: 0

Views: 199

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295500

You need the yarn start to be run by the copy of zsh, not run after that copy of zsh exits (which is what your code does now).

Consider using a heredoc or the -c argument to pass the code you want zsh to run on zsh's stdin:

#!/bin/sh

# ''|| exit'' prevents need to use && to connect to later commands
cd /Users/tillman/t-root/dev/apps/actual-server || exit

exec /usr/bin/arch -x86_64 /bin/zsh --login -c 'exec yarn start'

The execs are a performance enhancement, replacing the original shell with zsh, and then replacing the copy of zsh with a copy of yarn, instead of fork()ing subprocesses in which to run zsh and then yarn. (This also makes sending a signal to your script deliver that signal direct to yarn).

Upvotes: 1

Related Questions