寻求Ubuntu/OSX下可配置运行服务与测试并自动终止服务的通用工具
Great question! I totally get wanting to skip writing custom bash scripts when a config-based approach can keep things cleaner and more maintainable. Here are your best options for Ubuntu and macOS:
1. npm Scripts (No extra tools needed for JS/TS projects)
If your project already uses npm, yarn, or pnpm, this is the most seamless choice. You can define everything directly in your package.json without writing standalone scripts:
First, install wait-on (a tiny utility to wait for your server to be ready):
npm install --save-dev wait-on
Then add these scripts to your package.json:
{ "scripts": { "serve": "serve ./public", "test:chrome": "./run-chrome-tests.sh", "test:e2e": "npm run serve & PID=$!; wait-on http://localhost:3000; npm run test:chrome; kill $PID" } }
When you run npm run test:e2e, it will:
- Start the
serveprocess in the background and save its PID - Wait until
http://localhost:3000(adjust the port if yourserveuses a different one) is reachable - Run your Chrome tests
- Automatically kill the
serveprocess once tests finish
2. Makefile (Universal, no extra dependencies)
If you’re not working with a JS project, a Makefile is a classic, dependency-free way to define this workflow. Create a file named Makefile in your project root:
.PHONY: serve test-e2e serve: serve ./public test-e2e: @echo "Starting server in background..." serve ./public & PID=$$!; \ until curl -s http://localhost:3000 > /dev/null; do sleep 1; done; \ echo "Server ready — running tests..."; \ ./run-chrome-tests.sh; \ kill $$PID; \ echo "Tests complete — server stopped."
Just run make test-e2e to trigger the full workflow. The curl loop waits for the server to be responsive, and we clean up the server process automatically after tests.
3. Taskfile (Modern, readable alternative to Makefile)
If you prefer a more intuitive, YAML-based syntax over Makefile’s quirks, Taskfile is a great option. First install Task:
- On Ubuntu:
sudo apt install task - On macOS:
brew install go-task/tap/go-task
Then create a Taskfile.yml in your project root:
version: '3' tasks: serve: cmds: - serve ./public test-e2e: cmds: - | echo "Starting server..." serve ./public & PID=$! until curl -s http://localhost:3000 > /dev/null; do sleep 1; done echo "Running Chrome tests..." ./run-chrome-tests.sh kill $PID echo "All done — server terminated."
Run the workflow with task test-e2e — the syntax is more approachable for folks who don’t use Makefiles regularly.
All three of these options let you handle the workflow with configuration rather than writing a full bash script, so you can keep your setup clean and maintainable.
内容的提问来源于stack exchange,提问作者Ashley Coolman

