I am developing a node.js script which scans BLE advertisements using @abandonware/noble
.
Platform:
The script causes a segfault when not run with sudo
and I found no way to prevent this. The noble documentation mentions that if you want bluetooth scanning as regular user you need to set privileges with:
sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
but that does not make a change.
So I want to check in the script if it runs with admin privileges. I tried with process.getuid()
as written here but that always returns 1000
no matter if ran with sudo
or not.
How can I check if the script runs with sudo
?
you can use the os.userInfo()
.uid method to check the user id of the current user.
const os = require('os');
const user = os.userInfo().uid;
if (user === 0) {
console.log('Running with root privileges');
} else {
console.log('Not running with root privileges');
}
Keep in mind, that the scripts must be executed with sudo in order for it to run with root permissions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.